---
title: "Supplementary statistical analyses for 'Can courts in non-democracies deter election fraud? De jure judicial independence, political competition, and election integrity'"
output: html_notebook
author: Cole J. Harvey
date: 12/28/2021
---

```{r setup}
library(tidyverse)
library(lme4)
library(lmtest)
library(ggplot2)
library(ggeffects)
library(interplot)
library(interactions)
library(interplot.medline)
library(interflex)
library(stargazer)
library(wfe)
library(plm)
library(ebal)
library(cowplot)
library(sjPlot)
library(fastDummies)
library(vdemdata)

###Loading final dataset###
#vdem.nodems.elections <- read.csv("./Data/dataset_final.csv") #Includes CCP data, excludes countries that code as liberal democracies and/or have parcomp < 5

#####Loading dataset####
vdem.nodems <- read.csv("harvey_courts_manipulation_appendix_data.csv")

### Comparative Constitutions Project transformations
vdem.nodems <- vdem.nodems %>% mutate(ccp_positive_reform_all = ifelse(ccp_jidiff_total_lag >= 1, 1, 0))
vdem.nodems <- vdem.nodems %>% mutate(ccp_positive_reform_selrem = ifelse(ccp_jidiff_selrem_lag >= 1, 1, 0))

vdem.nodems <- vdem.nodems %>% mutate(ccp_positive_reform_all_2lag = ifelse(ccp_jidiff_total_2lag >= 1, 1, 0))
vdem.nodems <- vdem.nodems %>% mutate(ccp_positive_reform_selrem_2lag = ifelse(ccp_jidiff_selrem_2lag >= 1, 1, 0))

vdem.nodems <- vdem.nodems %>% mutate(election.year = ifelse(is.na(v2elirreg.inv) == F, 1, 0))


## Access variables


vdem.nodems <- vdem.nodems %>% mutate(ccp_positive_reform_access = ifelse(ccp_citizen_access_difference >= 1, 1, 0))



##Needed variables from vdem

vdem <- vdemdata::vdem
vdem <- vdem %>% dplyr::select(v2x_regime, v2x_clphy, e_migdpgro, v2x_cspart, v2elvotlrg, country_id, year, e_democracy_trans)
vdem <- vdem %>% dplyr::filter(year >= 1944)
vdem <- vdem %>% mutate(COWcode = countrycode::countrycode(sourcevar = country_id, origin = "vdem", destination = "cown"))

vdem <- vdem %>% mutate(COWyear = paste(COWcode, year, sep = "_"))

vdem.nodems <- vdem.nodems %>% left_join(vdem, by = "COWyear")

vdem.nodems <- vdem.nodems %>% rename(COWcode = COWcode.x)
vdem.nodems$COW.factor <- factor(vdem.nodems$COWcode)


##Subset to election years
vdem.nodems.elections <- subset(vdem.nodems, is.na(vdem.nodems$v2elirreg.inv)==FALSE)


###Coding presidential elections as majoritarian
vdem.nodems.elections <- vdem.nodems.elections %>% mutate(v2elparlel = ifelse(elexec == 1, 0, v2elparlel))

##Dummy variables for electoral system
vdem.nodems.elections <- fastDummies::dummy_cols(vdem.nodems.elections, select_columns = "v2elparlel")

vdem.nodems.elections$year.factor <- factor(vdem.nodems.elections$year.x)

vdem.nodems.elections <- vdem.nodems.elections %>% filter(parcomp.lag >= 0)
vdem.nodems.elections <- vdem.nodems.elections %>% filter(is.na(reform_positive.lag) == F)

##Transition


vdem.nodems <- vdem.nodems %>% mutate(positive_dem_trans = ifelse(is.na(e_democracy_trans)== T, NA, ifelse(e_democracy_trans == 1, 1, 0)))
vdem.nodems <- vdem.nodems %>% group_by(COWcode) %>% mutate(positive_dem_trans_3lead = dplyr::lead(positive_dem_trans, 3))
vdem.nodems <- vdem.nodems %>% group_by(COWcode) %>% mutate(positive_dem_trans_5lead = dplyr::lead(positive_dem_trans, 5))

vdem.nodems <- vdem.nodems %>% group_by(COWcode) %>% mutate(elcompindex.m.3lead = dplyr::lead(elcompindex.m, 3))
vdem.nodems <- vdem.nodems %>% group_by(COWcode) %>% mutate(election.year.3lead = dplyr::lead(election.year, 3))
vdem.nodems <- vdem.nodems %>% group_by(COWcode) %>% mutate(e_democracy_duration.3lead = dplyr::lead(e_democracy_duration, 3))
vdem.nodems <- vdem.nodems %>% group_by(COWcode) %>% mutate(e_migdppcln.3lead = dplyr::lead(e_migdppcln, 3))
vdem.nodems <- vdem.nodems %>% group_by(COWcode) %>% mutate(elcompindex.m.3lead = dplyr::lead(elcompindex.m, 3))


vdem.nodems <- vdem.nodems %>% group_by(COWcode) %>% mutate(elcompindex.m.5lead = dplyr::lead(elcompindex.m, 5))
vdem.nodems <- vdem.nodems %>% group_by(COWcode) %>% mutate(election.year.5lead = dplyr::lead(election.year, 5))
vdem.nodems <- vdem.nodems %>% group_by(COWcode) %>% mutate(e_democracy_duration.5lead = dplyr::lead(e_democracy_duration, 5))
vdem.nodems <- vdem.nodems %>% group_by(COWcode) %>% mutate(e_migdppcln.5lead = dplyr::lead(e_migdppcln, 5))
vdem.nodems <- vdem.nodems %>% group_by(COWcode) %>% mutate(elcompindex.m.5lead = dplyr::lead(elcompindex.m, 5))

                                      

##Lagging election variables

vdem.nodems.elections <- vdem.nodems.elections %>% group_by(COWcode) %>% mutate(v2elirreg.inv.lag = dplyr::lag(v2elirreg.inv))
vdem.nodems.elections <- vdem.nodems.elections %>% group_by(COWcode) %>% mutate(v2elintim.inv.lag = dplyr::lag(v2elintim.inv))   
vdem.nodems.elections <- vdem.nodems.elections %>% group_by(COWcode) %>% mutate(v2eldommon.lag = dplyr::lag(v2eldommon)) 
vdem.nodems.elections <- vdem.nodems.elections %>% group_by(COWcode) %>% mutate(elexec.lag = dplyr::lag(elexec))   


##Leading election variables

vdem.nodems.elections <- vdem.nodems.elections %>% group_by(COWcode) %>% mutate(v2elirreg.inv.lead = dplyr::lead(v2elirreg.inv))
vdem.nodems.elections <- vdem.nodems.elections %>% group_by(COWcode) %>% mutate(v2elintim.inv.lead = dplyr::lead(v2elintim.inv))   
vdem.nodems.elections <- vdem.nodems.elections %>% group_by(COWcode) %>% mutate(v2eldommon.lead = dplyr::lead(v2eldommon)) 
vdem.nodems.elections <- vdem.nodems.elections %>% group_by(COWcode) %>% mutate(elexec.lead = dplyr::lead(elexec))   
vdem.nodems.elections <- vdem.nodems.elections %>% group_by(COWcode) %>% mutate(opposition.oversight.lead = dplyr::lead(v2lgoppart))   
vdem.nodems.elections <- vdem.nodems.elections %>% group_by(COWcode) %>% mutate(gdp.lead = dplyr::lead(e_migdppcln))
vdem.nodems.elections <- vdem.nodems.elections %>% group_by(COWcode) %>% mutate(v2elintmon.lead = dplyr::lead(v2elintmon))  
vdem.nodems.elections <- vdem.nodems.elections %>% group_by(COWcode) %>% mutate(v2elparlel_1.lead = dplyr::lead(v2elparlel_1))  
vdem.nodems.elections <- vdem.nodems.elections %>% group_by(COWcode) %>% mutate(v2elparlel_2.lead = dplyr::lead(v2elparlel_2))  

vdem.nodems.elections <- vdem.nodems.elections %>% group_by(COWcode)  %>% mutate(elcompindex.m.lead = dplyr::lead( elcompindex.m))

vdem.nodems.elections <- vdem.nodems.elections %>% group_by(COWcode)  %>% mutate(h_polcon3.lead = dplyr::lead( h_polcon3.lag))


## Additional lags

vdem.nodems.elections <- vdem.nodems.elections %>% mutate(winner.margin = ifelse(elexec == 1, v2elvotlrg/100, v2ellovtlg/100))
vdem.nodems.elections <- vdem.nodems.elections %>% group_by(country_id) %>% mutate(cspart.1lag = lag(v2x_cspart))
vdem.nodems.elections <- vdem.nodems.elections %>% group_by(country_id) %>% mutate(gdpgro.1lag = lag(e_migdpgro))
vdem.nodems.elections <- vdem.nodems.elections %>% group_by(country_id) %>% mutate(physinteg.1lag = lag(v2x_clphy))

vdem.nodems.elections <- fastDummies::dummy_cols(vdem.nodems.elections, select_columns = "v2x_regime")

vdem.nodems.elections <- vdem.nodems.elections %>% filter(v2x_regime_3 == 0)


##CCP mutations

vdem.nodems.elections <- vdem.nodems.elections %>% mutate(electoral_court = ifelse(oversght == 2 | oversght == 3, 1, 0))

vdem.nodems.elections <- vdem.nodems.elections %>% mutate(el_court_ind = ifelse(electoral_court == 1, ccp_jind_total_electoral, 0))



#Remove large vdem dataset
rm(vdem)


```




##Intimidation and vote-buying as DVs, using opposition oversight




```{r entropy balancing for oversight}
myvars <- c("COW.factor", "year.factor", "transitional", "v2elirreg.inv", "v2elintim.inv", "v2elvotbuy.inv", "lc.ind.2lag", "lji.2lag", "hc.ind.2lag", "oppaut.2lag", "elexec", "education.2lag",  "v2elparlel_1", "v2elparlel_2",                        "opposition.oversight.lag", "opp.oversight.2lag",
             "urban.2lag",  "v2elintmon", "pack.lag", "purge.lag",
            "country_name", "reform_positive.lag", "loggpdpc.2lag", "reform_negative.lag",
            "democracy.duration.2lag", "exec.respectcon.2lag", "leg.constraints.2lag", 
            "altinfo.2lag", "loggpdpc.lag")  

dataset.matching <- vdem.nodems.elections[myvars]
dataset.matching.complete <- na.omit(dataset.matching)


myvars <- c("democracy.duration.2lag", "oppaut.2lag",  "opp.oversight.2lag",
            "loggpdpc.2lag", "urban.2lag",
            "hc.ind.2lag",  "lc.ind.2lag", "transitional", "exec.respectcon.2lag", "altinfo.2lag", "education.2lag",
            "leg.constraints.2lag")  #"lji.lag"
ebal.covariates <- dataset.matching.complete[myvars]

ebal.test <- ebalance(Treatment=dataset.matching.complete$reform_positive.lag, X = ebal.covariates)




# means in treatment group data
t.means <- apply(ebal.covariates[dataset.matching.complete$reform_positive.lag==1,],2,mean)
# means in reweighted control group data
c.means.bal <- apply(ebal.covariates[dataset.matching.complete$reform_positive.lag==0,],2,weighted.mean,w=ebal.test$w)
# means in raw data control group data
c.means <- apply(ebal.covariates[dataset.matching.complete$reform_positive.lag==0,],2,mean)


dataset.matching.complete.controls <- subset(dataset.matching.complete, dataset.matching.complete$reform_positive.lag == 0)
dataset.matching.complete.controls <- data.frame(cbind(dataset.matching.complete.controls, ebal.test$w))

dataset.matching.complete.treat <- subset(dataset.matching.complete, dataset.matching.complete$reform_positive.lag == 1)
dataset.matching.complete.treat$ebal.test.w <- 1

dataset.matching.complete.w <- bind_rows(dataset.matching.complete.treat, dataset.matching.complete.controls)


```


```{r vote-buying and oversight}

elvb.posreform.oversight.int <- lm(v2elvotbuy.inv~reform_positive.lag
                                        + 
                                          opposition.oversight.lag +
                                      reform_positive.lag*opposition.oversight.lag
                                      + elexec + v2elparlel_1 + v2elparlel_2  +
                                              v2elintmon  +
                                               reform_negative.lag + loggpdpc.lag + purge.lag + pack.lag+
                                     COW.factor, 
                                           weights=ebal.test.w,
                                           data = dataset.matching.complete.w)
p.vb <- interplot(elvb.posreform.oversight.int, var1="reform_positive.lag", var2="opposition.oversight.lag",
                      hist=TRUE) + theme_bw() + labs(x="Opposition oversight (1-year lag)", y="Marginal effect" , 
                                                     title="Marginal effect of positive reform \non vote-buying") +
  geom_hline(yintercept=0, linetype=2)  #Same as above


p.vb  


```


```{r intimidation and oversight}

elintim.posreform.oversight.int <- lm(v2elintim.inv~reform_positive.lag +
                                                 opposition.oversight.lag +
                                      reform_positive.lag*opposition.oversight.lag
                                      + elexec + v2elparlel_1 + v2elparlel_2  +
                                                                                         v2elintmon  +
                                               reform_negative.lag + loggpdpc.lag + purge.lag + pack.lag+
                                     COW.factor, 
                                           weights=ebal.test.w,
                                           data = dataset.matching.complete.w)
summary(elintim.posreform.oversight.int)
p.intim <- interplot(elintim.posreform.oversight.int, var1="reform_positive.lag", var2="opposition.oversight.lag",
                      hist=TRUE) + theme_bw() + labs(x="Opposition oversight (1-year lag)", y="Marginal effect" , 
                                                     title="Marginal effect of positive reform \non intimidation") +
  geom_hline(yintercept=0, linetype=2)  #Same as above


p.intim  


```
## Producing Table A.2

```{r table alternative DVs}
stargazer(elintim.posreform.oversight.int, elvb.posreform.oversight.int,
          type="text",
          omit = c("COW.factor"))

```
## Producing Figure A.2

```{r plot for intim and vb}
p.combined.vbintim <- cowplot::plot_grid(p.intim, p.vb, align = "h", nrow = 1, label_size = 12, hjust = 0)
p.combined.vbintim
```


#CBPS


Elcomp
```{r cbps elcomp}
myvars <- c("COW.factor", "v2elirreg.inv",  "transitional", "democracy.duration.2lag", "oppaut.2lag",  "elcompindex.m.2lag", "elcompindex.m.lag",  "v2elintmon", "v2elparlel_1", "v2elparlel_2", "reform_negative.lag", 
            "loggpdpc.2lag", "urban.2lag",
            "hc.ind.2lag",  "lc.ind.2lag", "transitional", "exec.respectcon.2lag", "altinfo.2lag", "education.2lag",
            "leg.constraints.2lag", "reform_positive.lag", "elexec", "loggpdpc.2lag", "loggpdpc.lag", "purge.lag",
            "pack.lag")



dataset.matching <- vdem.nodems.elections[myvars]
dataset.matching.complete <- na.omit(dataset.matching)

###Finding scores/weights 

cbps.out <- CBPS::CBPS(reform_positive.lag~ democracy.duration.2lag + oppaut.2lag +
                   education.2lag + transitional +
                   elcompindex.m.2lag + 
                   loggpdpc.2lag + 
                   urban.2lag +
                   + hc.ind.2lag 
                 +  lc.ind.2lag
                 + exec.respectcon.2lag  +  altinfo.2lag + 
                   + leg.constraints.2lag, data=dataset.matching.complete)
summary(cbps.out)
CBPS::balance(cbps.out)

###Output models


model.cbps.elcomp <- lm(v2elirreg.inv~ reform_positive.lag*elcompindex.m.lag  + elexec +  v2elparlel_1 + v2elparlel_2 + 
                                        loggpdpc.lag + 
                                            v2elintmon +
                                            reform_negative.lag + purge.lag + pack.lag+
                                      COW.factor
                                        , weights=cbps.out$weights,
                                        data = cbps.out$data) 
#summary(model.cbps.elcomp)

p.cbps.irreg.elcomp <- interplot(model.cbps.elcomp, var1="reform_positive.lag", var2="elcompindex.m.lag",
                              hist=TRUE, adjCI = TRUE) + theme_bw() + labs(x="Lagged adjusted Polity score", y="Marginal effect",
                                                             title="Marginal effect of positive judicial reform \non voting irregularities") +
  geom_hline(yintercept=0, linetype=2) 
p.cbps.irreg.elcomp

```


```{r cbps for oversight}
myvars <- c("COW.factor", "year.factor", "transitional", "v2elirreg.inv", "v2elintim.inv", "lc.ind.2lag", "hc.ind.2lag", "oppaut.2lag", "elexec", "education.2lag",  "v2elparlel_1", "v2elparlel_2",
            "opposition.oversight.lag", "opp.oversight.2lag", "urban.2lag",  "v2elintmon", 
            "country_name", "reform_positive.lag", "loggpdpc.2lag", "reform_negative.lag",
            "democracy.duration.2lag", "exec.respectcon.2lag", "leg.constraints.2lag", 
            "altinfo.2lag", "loggpdpc.lag", "purge.lag", "pack.lag")

dataset.matching <- vdem.nodems.elections[myvars]
dataset.matching.complete <- na.omit(dataset.matching)

cbps.out <- CBPS::CBPS(reform_positive.lag~ democracy.duration.2lag + oppaut.2lag +
                   education.2lag +
                   opp.oversight.2lag + 
                   loggpdpc.2lag + 
                   urban.2lag +
                   + hc.ind.2lag 
                 +  lc.ind.2lag
                 + exec.respectcon.2lag  +  altinfo.2lag + 
                   + leg.constraints.2lag + transitional, data=dataset.matching.complete)
summary(cbps.out)
oversight.bal <- CBPS::balance(cbps.out)

## Producing Table A.4

stargazer(oversight.bal, 
          type="text", 
          omit = c("COWcode"))


model.elirreg.cbps.oppover <-  lm(v2elirreg.inv~reform_positive.lag + opposition.oversight.lag +
         reform_positive.lag*opposition.oversight.lag +  elexec +  v2elparlel_1 + v2elparlel_2 + 
                                        loggpdpc.lag + 
                                            v2elintmon +
                                            reform_negative.lag + purge.lag + pack.lag+
                                      COW.factor
                                            , weights=cbps.out$weights,
                                            data = cbps.out$data) 

p.cbps.irreg.oppover <- interplot(model.elirreg.cbps.oppover, var1="reform_positive.lag", var2="opposition.oversight.lag",
                                  hist=TRUE, adjCI = T) + theme_bw() + labs(x="Opposition oversight lagged", y="Marginal effect",
                                                                 title="Marginal effect of positive judicial reform \non voting irregularities") +
  geom_hline(yintercept=0, linetype=2)    #Negative sig


p.cbps.irreg.oppover

```




Polcon

```{r cbps polcon}
myvars <- c("COW.factor", "v2elirreg.inv", "transitional", "democracy.duration.2lag", "oppaut.2lag",  "h_polcon3.2lag", "h_polcon3.lag",  "v2elintmon", "v2elparlel_1", "v2elparlel_2", "reform_negative.lag", 
            "loggpdpc.2lag", "urban.2lag",
            "hc.ind.2lag",  "lc.ind.2lag", "transitional", "exec.respectcon.2lag", "altinfo.2lag", "education.2lag",
            "leg.constraints.2lag", "reform_positive.lag", "elexec", "loggpdpc.2lag", "loggpdpc.lag",
            "purge.lag", "pack.lag")



dataset.matching <- vdem.nodems.elections[myvars]
dataset.matching.complete <- na.omit(dataset.matching)

###Finding scores/weights 

cbps.out <- CBPS::CBPS(reform_positive.lag~ democracy.duration.2lag + oppaut.2lag +
                   education.2lag + transitional +
                   h_polcon3.2lag + 
                   loggpdpc.2lag + 
                   urban.2lag +
                   + hc.ind.2lag 
                 +  lc.ind.2lag
                 + exec.respectcon.2lag  +  altinfo.2lag + 
                   + leg.constraints.2lag, data=dataset.matching.complete)
summary(cbps.out)
CBPS::balance(cbps.out)

###Output models

model.elirreg.cbps.polcon <- lm(v2elirreg.inv~reform_positive.lag +
                                             h_polcon3.lag + reform_positive.lag*h_polcon3.lag   +  elexec +  v2elparlel_1 + v2elparlel_2 + 
                                        loggpdpc.lag + 
                                            v2elintmon +
                                            reform_negative.lag + purge.lag + pack.lag+
                                      COW.factor
                                        , weights=cbps.out$weights,
                                        data = cbps.out$data) 
#summary(model.elirreg.cbps.polcon)

p.cbps.irreg.polcon <- interplot(model.elirreg.cbps.polcon, var1="reform_positive.lag", var2="h_polcon3.lag",
                              hist=TRUE, adjCI = TRUE) + theme_bw() + labs(x="Lagged 'polcon'", y="Marginal effect",
                                                             title="Marginal effect of positive judicial reform \non voting irregularities") +
  geom_hline(yintercept=0, linetype=2) 


p.cbps.irreg.polcon
```
## Producing Table A.3

```{r CBPS table, include=FALSE}
stargazer(model.cbps.elcomp,  model.elirreg.cbps.oppover, model.elirreg.cbps.polcon, 
          type="text", 
              omit = c("COW.factor"))
```

## Producing Figure A.3 

```{r cbps combined plot}
p.combined.cbps <- cowplot::plot_grid(p.cbps.irreg.elcomp, p.cbps.irreg.oppover, p.cbps.irreg.polcon, align = "h", nrow = 3, label_size = 12, hjust = 0)


p.combined.cbps
```


# Selection logits

## Producing Table A.5

```{r, include=F}
m.selection1 <- glm(reform_positive ~ democracy.duration.2lag + oppaut.2lag + 
                      elcompindex.m.2lag +
            loggpdpc.2lag + urban.2lag +
            hc.ind.2lag +  lc.ind.2lag + transitional + exec.respectcon.2lag +
              altinfo.2lag + education.2lag + leg.constraints.2lag, 
            data = vdem.nodems.elections,
            family=binomial(link="logit"))
summary(m.selection1)

m.selection2 <- glm(reform_positive ~ democracy.duration.2lag + oppaut.2lag + 
                      opp.oversight.2lag +
            loggpdpc.2lag + urban.2lag +
            hc.ind.2lag +  lc.ind.2lag + transitional + exec.respectcon.2lag +
              altinfo.2lag + education.2lag + leg.constraints.2lag, 
            data = vdem.nodems.elections,
            family=binomial(link="logit"))
summary(m.selection2)


m.selection3 <- glm(reform_positive ~ democracy.duration.2lag + oppaut.2lag + 
                      h_polcon3.2lag +
            loggpdpc.2lag + urban.2lag +
            hc.ind.2lag +  lc.ind.2lag + transitional + exec.respectcon.2lag +
              altinfo.2lag + education.2lag + leg.constraints.2lag, 
            data = vdem.nodems.elections,
            family=binomial(link="logit"))
summary(m.selection3)



stargazer(m.selection1, m.selection2, m.selection3, digits = 2, 
          type="text")
```


# CCP treatments


```{r ebal elcomp ccp, include=FALSE}

myvars <- c("COW.factor", "year", "transitional", "v2elirreg.inv", "v2elintim.inv", "lc.ind.2lag",  "hc.ind.2lag", "oppaut.2lag", "elexec", "education.2lag",  "v2elparlel_1", "v2elparlel_2", "lji.2lag", "v2elparlel_3", "purge.lag", "pack.lag",
            "elcompindex.m.lag", "elcompindex.m.2lag", "urban.2lag",  "v2elintmon", 
            "country_name", "ccp_positive_reform_selrem", "loggpdpc.2lag", "reform_negative.lag",
            "democracy.duration.2lag", "exec.respectcon.2lag", "leg.constraints.2lag", 
            "altinfo.2lag", "loggpdpc.lag") 

dataset.matching <- vdem.nodems.elections[myvars]
dataset.matching.complete <- na.omit(dataset.matching)


myvars <- c("democracy.duration.2lag", "oppaut.2lag",  "elcompindex.m.2lag", "lji.2lag",
             "loggpdpc.2lag", "urban.2lag",
            "hc.ind.2lag",  "lc.ind.2lag", "transitional", "exec.respectcon.2lag", "altinfo.2lag", "education.2lag",
            "leg.constraints.2lag") 
ebal.covariates <- dataset.matching.complete[myvars]

ebal.test <- ebalance(Treatment=dataset.matching.complete$ccp_positive_reform_selrem, X = ebal.covariates)



# means in treatment group data
t.means <- apply(ebal.covariates[dataset.matching.complete$ccp_positive_reform_selrem==1,],2,mean)
# means in reweighted control group data
c.means.bal <- apply(ebal.covariates[dataset.matching.complete$ccp_positive_reform_selrem==0,],2,weighted.mean,w=ebal.test$w)
# means in raw data control group data
c.means <- apply(ebal.covariates[dataset.matching.complete$ccp_positive_reform_selrem==0,],2,mean)




dataset.matching.complete.controls <- subset(dataset.matching.complete, dataset.matching.complete$ccp_positive_reform_selrem == 0)
dataset.matching.complete.controls <- data.frame(cbind(dataset.matching.complete.controls, ebal.test$w))


dataset.matching.complete.treat <- subset(dataset.matching.complete, dataset.matching.complete$ccp_positive_reform_selrem == 1)
dataset.matching.complete.treat$ebal.test.w <- 1

dataset.matching.complete.w <- bind_rows(dataset.matching.complete.treat, dataset.matching.complete.controls)


###regression



mm.elirreg.posreform.elcomp.all.ccp <- lm(v2elirreg.inv~ccp_positive_reform_selrem
                                        + 
                                          elcompindex.m.lag +
                                      ccp_positive_reform_selrem*elcompindex.m.lag
                                      + elexec + v2elparlel_1 + v2elparlel_2 + 
                                          loggpdpc.lag + 
                                              v2elintmon +
                                          reform_negative.lag+ purge.lag + pack.lag+
                                     COW.factor,  
                                            weights=ebal.test.w,
                                            data = dataset.matching.complete.w)
#summary(mm.elirreg.posreform.elcomp.all.ccp)


```

```{r}
p2.elcomp.ccp <- interplot(mm.elirreg.posreform.elcomp.all.ccp, var1="ccp_positive_reform_selrem", var2="elcompindex.m.lag",
                hist=TRUE, adjCI = TRUE) + theme_bw() + labs(x="Political openness \n(1-year lag)", y="Marginal effect") +
  geom_hline(yintercept=0, linetype=2)   
p2.elcomp.ccp
```



```{r include=FALSE}
### Legislative opposition oversight
myvars <- c("COW.factor", "year.factor", "transitional", "v2elirreg.inv", "v2elintim.inv", "lc.ind.2lag", 
            "hc.ind.2lag", "oppaut.2lag", "elexec", "education.2lag",  "lji.2lag", "pack.lag", "purge.lag",
            "opposition.oversight.lag", "opp.oversight.2lag", "urban.2lag", "v2elmulpar", "v2elintmon", 
            "country_name", "ccp_positive_reform_selrem", "loggpdpc.2lag", "reform_negative.lag",
            "democracy.duration.2lag", "exec.respectcon.2lag", "leg.constraints.2lag", 
            "altinfo.2lag", "loggpdpc.lag", "years.since.election",  "v2elparlel_1", "v2elparlel_2",  "v2elparlel_3")  

dataset.matching <- vdem.nodems.elections[myvars]
dataset.matching.complete <- na.omit(dataset.matching)


myvars <- c("democracy.duration.2lag", "oppaut.2lag",  "opp.oversight.2lag", "lji.2lag",
            "loggpdpc.2lag", "urban.2lag",
            "hc.ind.2lag",  "lc.ind.2lag", "transitional", "exec.respectcon.2lag", "altinfo.2lag", "education.2lag",
            "leg.constraints.2lag") 
ebal.covariates <- dataset.matching.complete[myvars]


ebal.test <- ebalance(Treatment=dataset.matching.complete$ccp_positive_reform_selrem, X = ebal.covariates)




# means in treatment group data
t.means <- apply(ebal.covariates[dataset.matching.complete$ccp_positive_reform_selrem==1,],2,mean)
# means in reweighted control group data
c.means.bal <- apply(ebal.covariates[dataset.matching.complete$ccp_positive_reform_selrem==0,],2,weighted.mean,w=ebal.test$w)
# means in raw data control group data
c.means <- apply(ebal.covariates[dataset.matching.complete$ccp_positive_reform_selrem==0,],2,mean)


dataset.matching.complete.controls <- subset(dataset.matching.complete, dataset.matching.complete$ccp_positive_reform_selrem == 0)
dataset.matching.complete.controls <- data.frame(cbind(dataset.matching.complete.controls, ebal.test$w))


dataset.matching.complete.treat <- subset(dataset.matching.complete, dataset.matching.complete$ccp_positive_reform_selrem == 1)
dataset.matching.complete.treat$ebal.test.w <- 1

dataset.matching.complete.w <- bind_rows(dataset.matching.complete.treat, dataset.matching.complete.controls)


###regression
elirreg.posreform.oversight.base.ccp <- lm(v2elirreg.inv~ccp_positive_reform_selrem +
                                               opposition.oversight.lag + elexec + 
                                           loggpdpc.lag + 
                                               v2elintmon + 
                                            v2elparlel_1 + v2elparlel_2 +  reform_negative.lag + purge.lag + pack.lag+
                                         COW.factor 
                                             , weights=ebal.test.w,
                                             data = dataset.matching.complete.w)
#summary(elirreg.posreform.oversight.base.ccp)


mm.elirreg.posreform.oversight.ccp <- lm(v2elirreg.inv~ 
                                              ccp_positive_reform_selrem*opposition.oversight.lag   + elexec + 
                                         loggpdpc.lag + 
                                              v2elintmon +
                                              v2elparlel_1 + v2elparlel_2   + reform_negative.lag + purge.lag + pack.lag+
                                      COW.factor, 
                                           weights=ebal.test.w,
                                           data = dataset.matching.complete.w)
#summary(mm.elirreg.posreform.oversight.ccp)

```

```{r}
p2.legov.ccp <- interplot(mm.elirreg.posreform.oversight.ccp, var1="ccp_positive_reform_selrem", var2="opposition.oversight.lag",
                hist=TRUE, adjCI = TRUE) + theme_bw() + labs(x="Opposition oversight \n(1-year lag)", y="Marginal effect") +
  geom_hline(yintercept=0, linetype=2)   
p2.legov.ccp
```



```{r ebal polcon, include=FALSE}
vdem.nodems.elections <- vdem.nodems.elections %>% mutate(scale.polcon.lag = log(h_polcon3.lag))
myvars <- c("COW.factor", "year.factor", "transitional", "v2elirreg.inv", "v2elintim.inv", "lc.ind.2lag", "lji.2lag", "hc.ind.2lag", "oppaut.2lag", "elexec", "education.2lag",  "v2elparlel_1", "v2elparlel_2",                        "h_polcon3.lag", "h_polcon3.2lag",
             "urban.2lag",  "v2elintmon", "pack.lag", "purge.lag",
            "country_name", "ccp_positive_reform_selrem", "loggpdpc.2lag", "reform_negative.lag",
            "democracy.duration.2lag", "exec.respectcon.2lag", "leg.constraints.2lag", 
            "altinfo.2lag", "loggpdpc.lag")  

dataset.matching <- vdem.nodems.elections[myvars]
dataset.matching.complete <- na.omit(dataset.matching)


myvars <- c("democracy.duration.2lag", "oppaut.2lag",  "h_polcon3.2lag", "lji.2lag",
             "loggpdpc.2lag", "urban.2lag",
            "hc.ind.2lag",  "lc.ind.2lag", "transitional", "exec.respectcon.2lag", "altinfo.2lag", "education.2lag",
            "leg.constraints.2lag") 
ebal.covariates <- dataset.matching.complete[myvars]

ebal.test <- ebalance(Treatment=dataset.matching.complete$ccp_positive_reform_selrem, X = ebal.covariates)



# means in treatment group data
t.means <- apply(ebal.covariates[dataset.matching.complete$ccp_positive_reform_selrem==1,],2,mean)
# means in reweighted control group data
c.means.bal <- apply(ebal.covariates[dataset.matching.complete$ccp_positive_reform_selrem==0,],2,weighted.mean,w=ebal.test$w)
# means in raw data control group data
c.means <- apply(ebal.covariates[dataset.matching.complete$ccp_positive_reform_selrem==0,],2,mean)



dataset.matching.complete.controls <- subset(dataset.matching.complete, dataset.matching.complete$ccp_positive_reform_selrem == 0)
dataset.matching.complete.controls <- data.frame(cbind(dataset.matching.complete.controls, ebal.test$w))


dataset.matching.complete.treat <- subset(dataset.matching.complete, dataset.matching.complete$ccp_positive_reform_selrem == 1)
dataset.matching.complete.treat$ebal.test.w <- 1

dataset.matching.complete.w <- bind_rows(dataset.matching.complete.treat, dataset.matching.complete.controls)


###regression
elirreg.posreform.polcon.base <- lm(v2elirreg.inv~ccp_positive_reform_selrem +
                                        
                                        h_polcon3.lag  + elexec +  v2elparlel_1 + v2elparlel_2  +
                                                                                    v2elintmon +
                                           reform_negative.lag + loggpdpc.lag + purge.lag + pack.lag+
                                      COW.factor, 
                                    weights=ebal.test.w,  
                                    data = dataset.matching.complete.w)
#summary(elirreg.posreform.polity.base)


mm.elirreg.posreform.polcon.ccp <- lm(v2elirreg.inv~ccp_positive_reform_selrem
                                        + 
                                          h_polcon3.lag +
                                      ccp_positive_reform_selrem*h_polcon3.lag
                                      + elexec + v2elparlel_1 + v2elparlel_2  +
                                              v2elintmon  +
                                               reform_negative.lag + loggpdpc.lag + purge.lag + pack.lag+
                                     COW.factor,  
                                            weights=ebal.test.w,
                                            data = dataset.matching.complete.w)
#summary(mm.elirreg.posreform.polcon.ccp)


```




```{r}
p4.polcon.ccp <- interplot(mm.elirreg.posreform.polcon.ccp, var1="ccp_positive_reform_selrem", var2="h_polcon3.lag",
                hist=TRUE, adjCI = TRUE) + theme_bw() + labs(x="Political constraints (1-year lag)", y="Marginal effect") +
  geom_hline(yintercept=0, linetype=2)   
p4.polcon.ccp
```
## Producing Table A.6

```{r CCP table, include=FALSE}
stargazer(mm.elirreg.posreform.elcomp.all.ccp,  mm.elirreg.posreform.oversight.ccp, mm.elirreg.posreform.polcon.ccp, 
          type="text", 
              omit = c("COW.factor"))
```


## Producing Figure A.5 

```{r CCP combined plot}
p.combined.ccp <- cowplot::plot_grid(p2.elcomp.ccp, p2.legov.ccp, p4.polcon.ccp, align = "h", nrow = 2, label_size = 12, hjust = 0)


p.combined.ccp
```


# Future election manipulation


Political openness
```{r}
myvars <- c("COW.factor", "transitional", "lc.ind.2lag", 
            "hc.ind.2lag", "oppaut.2lag", "education.2lag",  "lji.2lag", "pack.lag", "purge.lag",
            "urban.2lag",  
            "country_name", "reform_positive.lag", "loggpdpc.2lag", "reform_negative.lag",
            "democracy.duration.2lag", "exec.respectcon.2lag", "leg.constraints.2lag", 
            "altinfo.2lag", "elcompindex.m.lead", "elexec.lead", 
                                           "gdp.lead",
                                               "v2elintmon.lead", 
                                            "v2elparlel_1.lead", "v2elparlel_2.lead", "v2elirreg.inv.lead", "elcompindex.m.2lag") 

dataset.matching <- vdem.nodems.elections[myvars]
dataset.matching.complete <- na.omit(dataset.matching)


myvars <- c("democracy.duration.2lag", "oppaut.2lag",  "elcompindex.m.2lag", "lji.2lag",
            "loggpdpc.2lag", "urban.2lag",
            "hc.ind.2lag",  "lc.ind.2lag", "transitional", "exec.respectcon.2lag", "altinfo.2lag", "education.2lag",
            "leg.constraints.2lag") 
ebal.covariates <- dataset.matching.complete[myvars]


ebal.test <- ebalance(Treatment=dataset.matching.complete$reform_positive.lag, X = ebal.covariates)




# means in treatment group data
t.means <- apply(ebal.covariates[dataset.matching.complete$reform_positive.lag==1,],2,mean)
# means in reweighted control group data
c.means.bal <- apply(ebal.covariates[dataset.matching.complete$reform_positive.lag==0,],2,weighted.mean,w=ebal.test$w)
# means in raw data control group data
c.means <- apply(ebal.covariates[dataset.matching.complete$reform_positive.lag==0,],2,mean)


dataset.matching.complete.controls <- subset(dataset.matching.complete, dataset.matching.complete$reform_positive.lag == 0)
dataset.matching.complete.controls <- data.frame(cbind(dataset.matching.complete.controls, ebal.test$w))


dataset.matching.complete.treat <- subset(dataset.matching.complete, dataset.matching.complete$reform_positive.lag == 1)
dataset.matching.complete.treat$ebal.test.w <- 1

dataset.matching.complete.w <- bind_rows(dataset.matching.complete.treat, dataset.matching.complete.controls)



###regression
elirreglag.posreform.elcomp.lead <- lm(v2elirreg.inv.lead~reform_positive.lag*                                               elcompindex.m.lead + elexec.lead   +
                                           gdp.lead + 
                                               v2elintmon.lead + 
                                            v2elparlel_1.lead + v2elparlel_2.lead +
                                            +  reform_negative.lag + purge.lag + pack.lag+
                                         COW.factor
                                             , weights=ebal.test.w,
                                             data = dataset.matching.complete.w)
summary(elirreglag.posreform.elcomp.lead)





p.intertemp.elcomp <- interplot(elirreglag.posreform.elcomp.lead, var1 = "reform_positive.lag", var2 = "elcompindex.m.lead",  hist=TRUE, adjCI = TRUE) + theme_bw() + labs(x="Political competition (time 2)", y="Marginal effect") +
  geom_hline(yintercept=0, linetype=2)   
p.intertemp.elcomp
```


Legislative oversight

```{r}

myvars <- c("COW.factor", "transitional", "lc.ind.2lag", 
            "hc.ind.2lag", "oppaut.2lag", "education.2lag",  "lji.2lag", "pack.lag", "purge.lag",
            "urban.2lag",  
            "country_name", "reform_positive.lag", "loggpdpc.2lag", "reform_negative.lag",
            "democracy.duration.2lag", "exec.respectcon.2lag", "leg.constraints.2lag", 
            "altinfo.2lag", "opposition.oversight.lead", "elexec.lead", 
                                           "gdp.lead",
                                               "v2elintmon.lead", 
                                            "v2elparlel_1.lead", "v2elparlel_2.lead", "v2elirreg.inv.lead", "opp.oversight.2lag")  
dataset.matching <- vdem.nodems.elections[myvars]
dataset.matching.complete <- na.omit(dataset.matching)


myvars <- c("democracy.duration.2lag", "oppaut.2lag",  "opp.oversight.2lag", "lji.2lag",
            "loggpdpc.2lag", "urban.2lag",
            "hc.ind.2lag",  "lc.ind.2lag", "transitional", "exec.respectcon.2lag", "altinfo.2lag", "education.2lag",
            "leg.constraints.2lag")  
ebal.covariates <- dataset.matching.complete[myvars]

ebal.test <- ebalance(Treatment=dataset.matching.complete$reform_positive.lag, X = ebal.covariates)




# means in treatment group data
t.means <- apply(ebal.covariates[dataset.matching.complete$reform_positive.lag==1,],2,mean)
# means in reweighted control group data
c.means.bal <- apply(ebal.covariates[dataset.matching.complete$reform_positive.lag==0,],2,weighted.mean,w=ebal.test$w)
# means in raw data control group data
c.means <- apply(ebal.covariates[dataset.matching.complete$reform_positive.lag==0,],2,mean)


dataset.matching.complete.controls <- subset(dataset.matching.complete, dataset.matching.complete$reform_positive.lag == 0)
dataset.matching.complete.controls <- data.frame(cbind(dataset.matching.complete.controls, ebal.test$w))


dataset.matching.complete.treat <- subset(dataset.matching.complete, dataset.matching.complete$reform_positive.lag == 1)
dataset.matching.complete.treat$ebal.test.w <- 1

dataset.matching.complete.w <- bind_rows(dataset.matching.complete.treat, dataset.matching.complete.controls)



###regression
elirreglag.posreform.oversight.lead <- lm(v2elirreg.inv.lead~reform_positive.lag*opposition.oversight.lead + elexec.lead +
                                           gdp.lead + 
                                               v2elintmon.lead + 
                                            v2elparlel_1.lead + v2elparlel_2.lead +
                                            +  reform_negative.lag + purge.lag + pack.lag+
                                         COW.factor
                                             , weights=ebal.test.w,
                                             data = dataset.matching.complete.w)
summary(elirreglag.posreform.oversight.lead)





p.intertemp.osight <- interplot(elirreglag.posreform.oversight.lead, var1 = "reform_positive.lag", var2 = "opposition.oversight.lead",  hist=TRUE, adjCI = TRUE) + theme_bw() + labs(x="Opposition oversight (time 2)", y="Marginal effect") +
  geom_hline(yintercept=0, linetype=2)   
p.intertemp.osight
```



Political constraints
```{r}

myvars <- c("COW.factor", "transitional", "lc.ind.2lag", 
            "hc.ind.2lag", "oppaut.2lag", "education.2lag",  "lji.2lag", "pack.lag", "purge.lag",
            "urban.2lag",  
            "country_name", "reform_positive.lag", "loggpdpc.2lag", "reform_negative.lag",
            "democracy.duration.2lag", "exec.respectcon.2lag", "leg.constraints.2lag", 
            "altinfo.2lag", "h_polcon3.lead", "elexec.lead", 
                                           "gdp.lead",
                                               "v2elintmon.lead", 
                                            "v2elparlel_1.lead", "v2elparlel_2.lead", "v2elirreg.inv.lead", "h_polcon3.2lag")  

dataset.matching <- vdem.nodems.elections[myvars]
dataset.matching.complete <- na.omit(dataset.matching)


myvars <- c("democracy.duration.2lag", "oppaut.2lag",  "h_polcon3.2lag", "lji.2lag",
            "loggpdpc.2lag", "urban.2lag",
            "hc.ind.2lag",  "lc.ind.2lag", "transitional", "exec.respectcon.2lag", "altinfo.2lag", "education.2lag",
            "leg.constraints.2lag") 
ebal.covariates <- dataset.matching.complete[myvars]

ebal.test <- ebalance(Treatment=dataset.matching.complete$reform_positive.lag, X = ebal.covariates)




# means in treatment group data
t.means <- apply(ebal.covariates[dataset.matching.complete$reform_positive.lag==1,],2,mean)
# means in reweighted control group data
c.means.bal <- apply(ebal.covariates[dataset.matching.complete$reform_positive.lag==0,],2,weighted.mean,w=ebal.test$w)
# means in raw data control group data
c.means <- apply(ebal.covariates[dataset.matching.complete$reform_positive.lag==0,],2,mean)


dataset.matching.complete.controls <- subset(dataset.matching.complete, dataset.matching.complete$reform_positive.lag == 0)
dataset.matching.complete.controls <- data.frame(cbind(dataset.matching.complete.controls, ebal.test$w))


dataset.matching.complete.treat <- subset(dataset.matching.complete, dataset.matching.complete$reform_positive.lag == 1)
dataset.matching.complete.treat$ebal.test.w <- 1

dataset.matching.complete.w <- bind_rows(dataset.matching.complete.treat, dataset.matching.complete.controls)



###regression
elirreglag.posreform.polcon.lead <- lm(v2elirreg.inv.lead~reform_positive.lag*                                               h_polcon3.lead + elexec.lead +                                                   
                                           gdp.lead + 
                                               v2elintmon.lead + 
                                            v2elparlel_1.lead + v2elparlel_2.lead +
                                            +  reform_negative.lag + purge.lag + pack.lag+
                                         COW.factor
                                             , weights=ebal.test.w,
                                             data = dataset.matching.complete.w)
summary(elirreglag.posreform.polcon.lead)





p.intertemp.polcon <- interplot(elirreglag.posreform.polcon.lead, var1 = "reform_positive.lag", var2 = "h_polcon3.lead", hist=TRUE, adjCI = TRUE) + theme_bw() + labs(x="Political constraints (time 2)", y="Marginal effect") +
  geom_hline(yintercept=0, linetype=2)   
p.intertemp.polcon
```

## Producing Table B.1

```{r include=FALSE}
#Entropy balanced
stargazer(elirreglag.posreform.elcomp.lead, elirreglag.posreform.oversight.lead, elirreglag.posreform.polcon.lead,
          digits = 2, omit = c("COW.factor", "COWcode", "year.factor", "year"),
          type="text")


```
## Producing Figure B.1

```{r combining me plots}
p.combined.ebal.t2 <- cowplot::plot_grid(p.intertemp.elcomp, p.intertemp.osight, p.intertemp.polcon, align = "hv", nrow = 2, label_size = 12, hjust = 0)
p.combined.ebal.t2

```


# Transitions

```{r}


###Ebal
myvars <- c("COW.factor", "democracy.duration.2lag", "oppaut.2lag", "lji.2lag",
             "loggpdpc.2lag", "urban.2lag",
            "hc.ind.2lag",  "lc.ind.2lag", "transitional", "exec.respectcon.2lag", "altinfo.2lag", "education.2lag",
            "leg.constraints.2lag", "positive_dem_trans",  "reform_positive.lag", "elcompindex.m.lag", "e_democracy_duration",
                       "elcompindex.m.2lag",                       
                                      "e_migdppcln", 
                                            "reform_negative.lag", "purge.lag", "pack.lag", "election.year")  

dataset.matching <- vdem.nodems[myvars]
dataset.matching.complete <- na.omit(dataset.matching)


myvars <- c("democracy.duration.2lag", "oppaut.2lag",  "elcompindex.m.2lag", "lji.2lag",
             "loggpdpc.2lag", "urban.2lag",
            "hc.ind.2lag",  "lc.ind.2lag", "transitional", "exec.respectcon.2lag", "altinfo.2lag", "education.2lag",
            "leg.constraints.2lag") 
ebal.covariates <- dataset.matching.complete[myvars]

ebal.test <- ebalance(Treatment=dataset.matching.complete$reform_positive.lag, X = ebal.covariates)



# means in treatment group data
t.means <- apply(ebal.covariates[dataset.matching.complete$reform_positive.lag==1,],2,mean)
# means in reweighted control group data
c.means.bal <- apply(ebal.covariates[dataset.matching.complete$reform_positive.lag==0,],2,weighted.mean,w=ebal.test$w)
# means in raw data control group data
c.means <- apply(ebal.covariates[dataset.matching.complete$reform_positive.lag==0,],2,mean)


dataset.matching.complete.controls <- subset(dataset.matching.complete, dataset.matching.complete$reform_positive.lag == 0)
dataset.matching.complete.controls <- data.frame(cbind(dataset.matching.complete.controls, ebal.test$w))


dataset.matching.complete.treat <- subset(dataset.matching.complete, dataset.matching.complete$reform_positive.lag == 1)
dataset.matching.complete.treat$ebal.test.w <- 1

dataset.matching.complete.w <- bind_rows(dataset.matching.complete.treat, dataset.matching.complete.controls)





###Model




model.transition <- glm(positive_dem_trans ~  reform_positive.lag*elcompindex.m.lag*election.year +    e_democracy_duration +
                                              
                                      e_migdppcln + 
                                            reform_negative.lag + purge.lag + pack.lag+
                                      COW.factor, 
 weights = dataset.matching.complete.w$ebal.test.w,
                                          data = dataset.matching.complete.w)
#summary(model.transition)

   
```

```{r}
p.trans.1 <- sjPlot::plot_model(model.transition,  type="eff", terms = c("elcompindex.m.lag", "reform_positive.lag", "election.year"), title = "One-year lead", legend.title = "Positive reform", colors = "bw", show.data = F) + xlab("Political competition") + ylab("Predicted value of transition") + theme_bw()  + theme(legend.position = "none")
p.trans.1
```


3 years out

```{r}

myvars <- c("COW.factor", "democracy.duration.2lag", "oppaut.2lag", "lji.2lag",
             "loggpdpc.2lag", "urban.2lag",
            "hc.ind.2lag",  "lc.ind.2lag", "transitional", "exec.respectcon.2lag", "altinfo.2lag", "education.2lag",
            "leg.constraints.2lag", "positive_dem_trans_3lead",  "reform_positive.lag", "e_democracy_duration.3lead",
                       "elcompindex.m.2lag",  "elcompindex.m.3lead",                     
                                      "e_migdppcln.3lead", 
                                            "reform_negative.lag", "purge.lag", "pack.lag", "election.year.3lead") 

dataset.matching <- vdem.nodems[myvars]
dataset.matching.complete <- na.omit(dataset.matching)


myvars <- c("democracy.duration.2lag", "oppaut.2lag",  "elcompindex.m.2lag", "lji.2lag",
             "loggpdpc.2lag", "urban.2lag",
            "hc.ind.2lag",  "lc.ind.2lag", "transitional", "exec.respectcon.2lag", "altinfo.2lag", "education.2lag",
            "leg.constraints.2lag")  #"lji.lag"
ebal.covariates <- dataset.matching.complete[myvars]

ebal.test <- ebalance(Treatment=dataset.matching.complete$reform_positive.lag, X = ebal.covariates)



# means in treatment group data
t.means <- apply(ebal.covariates[dataset.matching.complete$reform_positive.lag==1,],2,mean)
# means in reweighted control group data
c.means.bal <- apply(ebal.covariates[dataset.matching.complete$reform_positive.lag==0,],2,weighted.mean,w=ebal.test$w)
# means in raw data control group data
c.means <- apply(ebal.covariates[dataset.matching.complete$reform_positive.lag==0,],2,mean)


dataset.matching.complete.controls <- subset(dataset.matching.complete, dataset.matching.complete$reform_positive.lag == 0)
dataset.matching.complete.controls <- data.frame(cbind(dataset.matching.complete.controls, ebal.test$w))


dataset.matching.complete.treat <- subset(dataset.matching.complete, dataset.matching.complete$reform_positive.lag == 1)
dataset.matching.complete.treat$ebal.test.w <- 1

dataset.matching.complete.w <- bind_rows(dataset.matching.complete.treat, dataset.matching.complete.controls)





###Model




model.transition.3lead <- glm(positive_dem_trans_3lead ~  reform_positive.lag*elcompindex.m.3lead*election.year.3lead +    e_democracy_duration.3lead +
                                              
                                      e_migdppcln.3lead + 
                                            reform_negative.lag + purge.lag + pack.lag+
                                        #+ transitional + altinfo.lag 
                                      COW.factor, 
 weights = dataset.matching.complete.w$ebal.test.w,
                                          data = dataset.matching.complete.w)
#summary(model.transition.3lead)


```
```{r}
p.trans.3 <- sjPlot::plot_model(model.transition.3lead,  type="eff", terms = c("elcompindex.m.3lead", "reform_positive.lag", "election.year.3lead"), title = "Three-year lead", legend.title = "Positive reform", colors = "bw", show.data = F) + xlab("Political competition") + ylab("Predicted value of transition") + theme_bw()  + theme(legend.position = "none")
p.trans.3
```

Five years out


```{r}

###Ebal
myvars <- c("COW.factor", "democracy.duration.2lag", "oppaut.2lag", "lji.2lag",
             "loggpdpc.2lag", "urban.2lag",
            "hc.ind.2lag",  "lc.ind.2lag", "transitional", "exec.respectcon.2lag", "altinfo.2lag", "education.2lag",
            "leg.constraints.2lag", "positive_dem_trans_5lead",  "reform_positive.lag", "e_democracy_duration.5lead",
                       "elcompindex.m.2lag",  "elcompindex.m.5lead",                     
                                      "e_migdppcln.5lead", 
                                            "reform_negative.lag", "purge.lag", "pack.lag", "election.year.5lead") 

dataset.matching <- vdem.nodems[myvars]
dataset.matching.complete <- na.omit(dataset.matching)


myvars <- c("democracy.duration.2lag", "oppaut.2lag",  "elcompindex.m.2lag", "lji.2lag",
             "loggpdpc.2lag", "urban.2lag",
            "hc.ind.2lag",  "lc.ind.2lag", "transitional", "exec.respectcon.2lag", "altinfo.2lag", "education.2lag",
            "leg.constraints.2lag")
ebal.covariates <- dataset.matching.complete[myvars]

ebal.test <- ebalance(Treatment=dataset.matching.complete$reform_positive.lag, X = ebal.covariates)



# means in treatment group data
t.means <- apply(ebal.covariates[dataset.matching.complete$reform_positive.lag==1,],2,mean)
# means in reweighted control group data
c.means.bal <- apply(ebal.covariates[dataset.matching.complete$reform_positive.lag==0,],2,weighted.mean,w=ebal.test$w)
# means in raw data control group data
c.means <- apply(ebal.covariates[dataset.matching.complete$reform_positive.lag==0,],2,mean)



dataset.matching.complete.controls <- subset(dataset.matching.complete, dataset.matching.complete$reform_positive.lag == 0)
dataset.matching.complete.controls <- data.frame(cbind(dataset.matching.complete.controls, ebal.test$w))


dataset.matching.complete.treat <- subset(dataset.matching.complete, dataset.matching.complete$reform_positive.lag == 1)
dataset.matching.complete.treat$ebal.test.w <- 1

dataset.matching.complete.w <- bind_rows(dataset.matching.complete.treat, dataset.matching.complete.controls)





###Model




model.transition.5lead <- glm(positive_dem_trans_5lead ~  reform_positive.lag*elcompindex.m.5lead*election.year.5lead +    e_democracy_duration.5lead +
                                              
                                      e_migdppcln.5lead + 
                                            reform_negative.lag + purge.lag + pack.lag+
                                      COW.factor, 
 weights = dataset.matching.complete.w$ebal.test.w,
                                          data = dataset.matching.complete.w)
#summary(model.transition.5lead)


```
```{r}
p.trans.5 <- sjPlot::plot_model(model.transition.5lead,  type="eff", terms = c("elcompindex.m.5lead", "reform_positive.lag", "election.year.5lead"), title = "Five-year lead", legend.title = "Positive reform", colors = "bw", show.data = F) + xlab("Political competition") + ylab("Predicted value of transition") + theme_bw()  + theme(legend.position = "none")
p.trans.5
```
## Producing Table B.2
```{r include=FALSE}
#Entropy balanced
stargazer(model.transition, model.transition.3lead, model.transition.5lead,
          digits = 2, omit = c("COW.factor", "COWcode", "year.factor"),
          type="text")


```


## Producing Figure B.2

```{r combining me plots}
p.combined.trans <- cowplot::plot_grid(p.trans.1, p.trans.3, p.trans.5, align = "hv", nrow = 3, label_size = 12, hjust = 0)
p.combined.trans


```


# Protest

```{r}
#To replicate this portion of the study, you must first have access to the NELDA dataset. Access to the data can be requested at https://nelda.co/#access (as of 12/27/2021).

nelda <- readxl::read_xls("NELDA.xls")

nelda <- nelda %>% rename(first.mp.election = nelda2)
nelda <- nelda %>% rename(protests = nelda29)
nelda <- nelda %>% rename(protests.fraud = nelda30)
nelda <- nelda %>% rename(inc.fav.cancel = nelda34)

nelda <- nelda %>% mutate(protests = ifelse(protests == "yes", 1, ifelse(protests == "no", 0, NA)))
nelda <- nelda %>% mutate(protests.fraud = ifelse(protests.fraud == "yes", 1, ifelse(protests.fraud == "no", 0, NA)))

nelda <- nelda %>% mutate(inc.fav.cancel = ifelse(inc.fav.cancel == "yes", 1, ifelse(inc.fav.cancel == "no", 0, NA)))
nelda <- nelda %>% mutate(inc.fav.cancel.protests = ifelse(nelda35 == "yes", 1, 0))
nelda <- nelda %>% mutate(opp.fav.cancel = ifelse(nelda32 == "yes", 1, 0))

##VDEM id
nelda <- nelda %>% mutate(country_id = countrycode::countrycode(sourcevar = ccode, origin = "gwn", destination = "cown"))
nelda <- nelda %>% mutate(COWyear = paste(country_id, year, sep = "_"))

#Cleaning dates

nelda <- nelda %>% mutate(mmdd = ifelse(mmdd < 1000, paste0(0, mmdd), mmdd))
nelda <- nelda %>% mutate(election_mdy = paste0(mmdd, year))
nelda <- nelda %>% mutate(election_mdy = lubridate::mdy(election_mdy))

#Mutations
nelda <- nelda %>% mutate(elexec = ifelse(types == "Executive", 1, 0))

#Merging

nelda <- nelda %>% filter(is.na(country_id) == FALSE)
nelda2 <- nelda %>% dplyr::select(COWyear, protests, protests.fraud, elexec)

vdem.nodems.nelda <- vdem.nodems.elections %>% left_join(nelda2, by = c("COWyear", "elexec"))  #Note, this creates duplicate observations when Nelda records multiple elections in one year (V-Dem does not capture this). 

vdem.nodems.nelda <- vdem.nodems.nelda %>% mutate(protests.fraud = ifelse(protests == 0 & is.na(protests.fraud)==T, 0, protests.fraud)) #This is because Nelda codes this as NA if "protests" == 0)


```


###Ebal for protest

```{r}
myvars <- c("country_id", "democracy.duration.2lag", "oppaut.2lag",  "elcompindex.m.2lag", "lji.2lag",
            "loggpdpc.2lag", "urban.2lag",
            "hc.ind.2lag",  "lc.ind.2lag", "transitional", "exec.respectcon.2lag", "altinfo.2lag", "education.2lag",
            "leg.constraints.2lag",
            "protests.fraud",
             "reform_positive.lag", "v2elirreg.inv", "elcompindex.m.lag", "winner.margin", "cspart.1lag", "gdpgro.1lag" , "elexec", "physinteg.1lag", "v2x_regime_0", 
             "v2x_regime_2", "v2x_regime_3") 

dataset.matching <- vdem.nodems.nelda[myvars]
dataset.matching.complete <- na.omit(dataset.matching)


myvars <- c("democracy.duration.2lag", "oppaut.2lag",  "elcompindex.m.2lag", "lji.2lag",
            "loggpdpc.2lag", "urban.2lag",
            "hc.ind.2lag",  "lc.ind.2lag", "transitional", "exec.respectcon.2lag", "altinfo.2lag", "education.2lag",
            "leg.constraints.2lag") 
ebal.covariates <- dataset.matching.complete[myvars]


ebal.test <- ebalance(Treatment=dataset.matching.complete$reform_positive.lag, X = ebal.covariates)




# means in treatment group data
t.means <- apply(ebal.covariates[dataset.matching.complete$reform_positive.lag==1,],2,mean)
# means in reweighted control group data
c.means.bal <- apply(ebal.covariates[dataset.matching.complete$reform_positive.lag==0,],2,weighted.mean,w=ebal.test$w)
# means in raw data control group data
c.means <- apply(ebal.covariates[dataset.matching.complete$reform_positive.lag==0,],2,mean)


dataset.matching.complete.controls <- subset(dataset.matching.complete, dataset.matching.complete$reform_positive.lag == 0)
dataset.matching.complete.controls <- data.frame(cbind(dataset.matching.complete.controls, ebal.test$w))


dataset.matching.complete.treat <- subset(dataset.matching.complete, dataset.matching.complete$reform_positive.lag == 1)
dataset.matching.complete.treat$ebal.test.w <- 1

dataset.matching.complete.w <- bind_rows(dataset.matching.complete.treat, dataset.matching.complete.controls)



model.3way.comp <- glm(protests.fraud ~ reform_positive.lag*v2elirreg.inv*elcompindex.m.lag + winner.margin + cspart.1lag + gdpgro.1lag + elexec + physinteg.1lag + v2x_regime_0 + v2x_regime_2 + v2x_regime_3 +  factor(country_id),
                      weights = ebal.test.w,
                      data = dataset.matching.complete.w)
summary(model.3way.comp)

p.protest1 <- sjPlot::plot_model(model.3way.comp, type = "pred", terms =  c("elcompindex.m.lag",   "v2elirreg.inv [-2.55, 2.86]", "reform_positive.lag"), title = "Marginal effects", legend.title = "Voting \nirregularities")  + xlab("Electoral competitiveness") + ylab("Post-election protest") 

p.protest1 <- p.protest1 + theme_bw()

```



Leg oversight

```{r}
myvars <- c("country_id", "democracy.duration.2lag", "oppaut.2lag",  "opp.oversight.2lag", "lji.2lag",
            "loggpdpc.2lag", "urban.2lag",
            "hc.ind.2lag",  "lc.ind.2lag", "transitional", "exec.respectcon.2lag", "altinfo.2lag", "education.2lag",
            "leg.constraints.2lag",
            "protests.fraud",
             "reform_positive.lag", "v2elirreg.inv", "opposition.oversight.lag", "winner.margin", "cspart.1lag", "gdpgro.1lag" , "elexec", "physinteg.1lag", "v2x_regime_0", 
             "v2x_regime_2", "v2x_regime_3") 

dataset.matching <- vdem.nodems.nelda[myvars]
dataset.matching.complete <- na.omit(dataset.matching)


myvars <- c("democracy.duration.2lag", "oppaut.2lag",  "opp.oversight.2lag", "lji.2lag",
            "loggpdpc.2lag", "urban.2lag",
            "hc.ind.2lag",  "lc.ind.2lag", "transitional", "exec.respectcon.2lag", "altinfo.2lag", "education.2lag",
            "leg.constraints.2lag")  
ebal.covariates <- dataset.matching.complete[myvars]

ebal.test <- ebalance(Treatment=dataset.matching.complete$reform_positive.lag, X = ebal.covariates)




# means in treatment group data
t.means <- apply(ebal.covariates[dataset.matching.complete$reform_positive.lag==1,],2,mean)
# means in reweighted control group data
c.means.bal <- apply(ebal.covariates[dataset.matching.complete$reform_positive.lag==0,],2,weighted.mean,w=ebal.test$w)
# means in raw data control group data
c.means <- apply(ebal.covariates[dataset.matching.complete$reform_positive.lag==0,],2,mean)


dataset.matching.complete.controls <- subset(dataset.matching.complete, dataset.matching.complete$reform_positive.lag == 0)
dataset.matching.complete.controls <- data.frame(cbind(dataset.matching.complete.controls, ebal.test$w))



dataset.matching.complete.treat <- subset(dataset.matching.complete, dataset.matching.complete$reform_positive.lag == 1)
dataset.matching.complete.treat$ebal.test.w <- 1

dataset.matching.complete.w <- bind_rows(dataset.matching.complete.treat, dataset.matching.complete.controls)



model.3way.oversight <- glm(protests.fraud ~ reform_positive.lag*v2elirreg.inv*opposition.oversight.lag + winner.margin + cspart.1lag + gdpgro.1lag + elexec + physinteg.1lag + v2x_regime_0 + v2x_regime_2 + v2x_regime_3 +  factor(country_id),
                      weights = ebal.test.w,
                      data = dataset.matching.complete.w)
summary(model.3way.oversight)

p.protest2 <- sjPlot::plot_model(model.3way.oversight, type = "pred", terms =  c( "opposition.oversight.lag", "v2elirreg.inv [-2.55, 2.86]", "reform_positive.lag"), title = "", show.legend = F) + xlab("Opposition oversight") + ylab("Post-election protest") 

p.protest2 <- p.protest2 + theme_bw()
```

Polcon

```{r}
myvars <- c("country_id", "democracy.duration.2lag", "oppaut.2lag",  "h_polcon3.2lag", "lji.2lag",
            "loggpdpc.2lag", "urban.2lag",
            "hc.ind.2lag",  "lc.ind.2lag", "transitional", "exec.respectcon.2lag", "altinfo.2lag", "education.2lag",
            "leg.constraints.2lag",
            "protests.fraud",
             "reform_positive.lag", "v2elirreg.inv", "h_polcon3.lag", "winner.margin", "cspart.1lag", "gdpgro.1lag" , "elexec", "physinteg.1lag", "v2x_regime_0", 
             "v2x_regime_2", "v2x_regime_3") 

dataset.matching <- vdem.nodems.nelda[myvars]
dataset.matching.complete <- na.omit(dataset.matching)


myvars <- c("democracy.duration.2lag", "oppaut.2lag",  "h_polcon3.2lag", "lji.2lag",
            "loggpdpc.2lag", "urban.2lag",
            "hc.ind.2lag",  "lc.ind.2lag", "transitional", "exec.respectcon.2lag", "altinfo.2lag", "education.2lag",
            "leg.constraints.2lag") 
ebal.covariates <- dataset.matching.complete[myvars]


ebal.test <- ebalance(Treatment=dataset.matching.complete$reform_positive.lag, X = ebal.covariates)




# means in treatment group data
t.means <- apply(ebal.covariates[dataset.matching.complete$reform_positive.lag==1,],2,mean)
# means in reweighted control group data
c.means.bal <- apply(ebal.covariates[dataset.matching.complete$reform_positive.lag==0,],2,weighted.mean,w=ebal.test$w)
# means in raw data control group data
c.means <- apply(ebal.covariates[dataset.matching.complete$reform_positive.lag==0,],2,mean)


dataset.matching.complete.controls <- subset(dataset.matching.complete, dataset.matching.complete$reform_positive.lag == 0)
dataset.matching.complete.controls <- data.frame(cbind(dataset.matching.complete.controls, ebal.test$w))


dataset.matching.complete.treat <- subset(dataset.matching.complete, dataset.matching.complete$reform_positive.lag == 1)
dataset.matching.complete.treat$ebal.test.w <- 1

dataset.matching.complete.w <- bind_rows(dataset.matching.complete.treat, dataset.matching.complete.controls)



model.3way.polcon <- glm(protests.fraud ~ reform_positive.lag*v2elirreg.inv*h_polcon3.lag + winner.margin + cspart.1lag + gdpgro.1lag + elexec + physinteg.1lag +  v2x_regime_0 + v2x_regime_2 + v2x_regime_3 +  factor(country_id),
                      weights = ebal.test.w,
                      data = dataset.matching.complete.w)
summary(model.3way.polcon)

p.protest3 <- sjPlot::plot_model(model.3way.polcon, type = "pred", terms =  c( "h_polcon3.lag", "v2elirreg.inv [-2.55, 2.86]", "reform_positive.lag"), title = "", show.legend = F) + xlab("Political constraints") + ylab("Post-election protest") 

p.protest3 <- p.protest3 + theme_bw()

```
## Producing Table B.3

```{r}
#Entropy balanced
stargazer(model.3way.comp, model.3way.oversight, model.3way.polcon,
          digits = 2, omit = c("country_id"),
          type="text")


```

## Producing Figure B.3

```{r}
p.combined.protest <- cowplot::plot_grid(p.protest1, p.protest2, p.protest3, align = "h", nrow = 3, label_size = 12, hjust = 0)
p.combined.protest


```






# Interacting with Cold War indicator


```{r ebal elcomp cw, include=FALSE}
vdem.nodems.elections <- vdem.nodems.elections %>% mutate(postCW = ifelse(year > 1991, 1, 0))


myvars <- c("COW.factor", "year", "transitional", "v2elirreg.inv", "v2elintim.inv", "lc.ind.2lag",  "hc.ind.2lag", "oppaut.2lag", "elexec", "education.2lag",  "v2elparlel_1", "v2elparlel_2", "lji.2lag", "v2elparlel_3", "purge.lag", "pack.lag",
            "elcompindex.m.lag", "elcompindex.m.2lag", "urban.2lag",  "v2elintmon", 
            "country_name", "reform_positive.lag", "loggpdpc.2lag", "reform_negative.lag",
            "democracy.duration.2lag", "exec.respectcon.2lag", "leg.constraints.2lag", 
            "altinfo.2lag", "loggpdpc.lag", "postCW") 

dataset.matching <- vdem.nodems.elections[myvars]
dataset.matching.complete <- na.omit(dataset.matching)


myvars <- c("democracy.duration.2lag", "oppaut.2lag",  "elcompindex.m.2lag", "lji.2lag",
             "loggpdpc.2lag", "urban.2lag",
            "hc.ind.2lag",  "lc.ind.2lag", "transitional", "exec.respectcon.2lag", "altinfo.2lag", "education.2lag",
            "leg.constraints.2lag")  
ebal.covariates <- dataset.matching.complete[myvars]

ebal.test <- ebalance(Treatment=dataset.matching.complete$reform_positive.lag, X = ebal.covariates)



# means in treatment group data
t.means <- apply(ebal.covariates[dataset.matching.complete$reform_positive.lag==1,],2,mean)
# means in reweighted control group data
c.means.bal <- apply(ebal.covariates[dataset.matching.complete$reform_positive.lag==0,],2,weighted.mean,w=ebal.test$w)
# means in raw data control group data
c.means <- apply(ebal.covariates[dataset.matching.complete$reform_positive.lag==0,],2,mean)



dataset.matching.complete.controls <- subset(dataset.matching.complete, dataset.matching.complete$reform_positive.lag == 0)
dataset.matching.complete.controls <- data.frame(cbind(dataset.matching.complete.controls, ebal.test$w))


dataset.matching.complete.treat <- subset(dataset.matching.complete, dataset.matching.complete$reform_positive.lag == 1)
dataset.matching.complete.treat$ebal.test.w <- 1

dataset.matching.complete.w <- bind_rows(dataset.matching.complete.treat, dataset.matching.complete.controls)




elirreg.posreform.cw <- lm(v2elirreg.inv~ elcompindex.m.lag +
                                      elcompindex.m.lag*reform_positive.lag*postCW
                                      + elexec + v2elparlel_1 + v2elparlel_2 + 
                                          loggpdpc.lag + 
                                          reform_negative.lag+ purge.lag + pack.lag+
                                     COW.factor,  
                                            weights=ebal.test.w,
                                            data = dataset.matching.complete.w)
#summary(elirreg.posreform.cw)


```

## Producing Table B.4

```{r}
stargazer(elirreg.posreform.cw,
          digits = 2, omit = c("COW.factor"),
          type="text")


```

## Producing Figure B.4

```{r plot ebal elcomp cw}

plot.cw <- sjPlot::plot_model(elirreg.posreform.cw,  type="eff", terms = c("elcompindex.m.lag", "reform_positive.lag", "postCW"), title = "", legend.title = "Positive reform", colors = "bw") + xlab("Political competition (1-year lag)") + ylab("Predicted value of \nvoting irregularities") + theme_bw() 

plot.cw
```



# Lagged manipulation in selection model


Elcomp

```{r ebal elcomp, include=FALSE}



myvars <- c("COW.factor", "year", "transitional", "v2elirreg.inv", "v2elintim.inv", "lc.ind.2lag",  "hc.ind.2lag", "oppaut.2lag", "elexec", "education.2lag",  "v2elparlel_1", "v2elparlel_2", "lji.2lag", "v2elparlel_3", "purge.lag", "pack.lag",
            "elcompindex.m.lag", "elcompindex.m.2lag", "urban.2lag",  "v2elintmon", 
            "country_name", "reform_positive.lag", "loggpdpc.2lag", "reform_negative.lag",
            "democracy.duration.2lag", "exec.respectcon.2lag", "leg.constraints.2lag", 
            "altinfo.2lag", "loggpdpc.lag", "v2elirreg.inv.lag") 

dataset.matching <- vdem.nodems.elections[myvars]
dataset.matching.complete <- na.omit(dataset.matching)


myvars <- c("democracy.duration.2lag", "oppaut.2lag",  "elcompindex.m.2lag", "lji.2lag",
             "loggpdpc.2lag", "urban.2lag",
            "hc.ind.2lag",  "lc.ind.2lag", "transitional", "exec.respectcon.2lag", "altinfo.2lag", "education.2lag",
            "leg.constraints.2lag", "v2elirreg.inv.lag")  
ebal.covariates <- dataset.matching.complete[myvars]

ebal.test <- ebalance(Treatment=dataset.matching.complete$reform_positive.lag, X = ebal.covariates)



# means in treatment group data
t.means <- apply(ebal.covariates[dataset.matching.complete$reform_positive.lag==1,],2,mean)
# means in reweighted control group data
c.means.bal <- apply(ebal.covariates[dataset.matching.complete$reform_positive.lag==0,],2,weighted.mean,w=ebal.test$w)
# means in raw data control group data
c.means <- apply(ebal.covariates[dataset.matching.complete$reform_positive.lag==0,],2,mean)



dataset.matching.complete.controls <- subset(dataset.matching.complete, dataset.matching.complete$reform_positive.lag == 0)
dataset.matching.complete.controls <- data.frame(cbind(dataset.matching.complete.controls, ebal.test$w))


dataset.matching.complete.treat <- subset(dataset.matching.complete, dataset.matching.complete$reform_positive.lag == 1)
dataset.matching.complete.treat$ebal.test.w <- 1

dataset.matching.complete.w <- bind_rows(dataset.matching.complete.treat, dataset.matching.complete.controls)





mm.elirreg.posreform.elcomp.lagman <- lm(v2elirreg.inv~reform_positive.lag
                                        + 
                                          elcompindex.m.lag +
                                      reform_positive.lag*elcompindex.m.lag
                                      + elexec + v2elparlel_1 + v2elparlel_2 + 
                                              # e_polity2 +
                                          loggpdpc.lag + 
                                              #e_miurbani + 
                                              v2elintmon +
                                          reform_negative.lag+ purge.lag + pack.lag+
                                     COW.factor,  
                                            weights=ebal.test.w,
                                            data = dataset.matching.complete.w)
#summary(mm.elirreg.posreform.elcomp.lagman)


```


```{r plot ebal elcomp}
p2.elcomp.lagman <- interplot(mm.elirreg.posreform.elcomp.lagman, var1="reform_positive.lag", var2="elcompindex.m.lag",
                hist=TRUE, adjCI = TRUE) + theme_bw() + labs(x="Political openness \n(1-year lag)", y="Marginal effect") +
  geom_hline(yintercept=0, linetype=2)   
p2.elcomp.lagman
```


Legislative oversight

```{r include=FALSE}
### Legislative opposition oversight
myvars <- c("COW.factor", "year.factor", "transitional", "v2elirreg.inv", "v2elintim.inv", "lc.ind.2lag", 
            "hc.ind.2lag", "oppaut.2lag", "elexec", "education.2lag",  "lji.2lag", "pack.lag", "purge.lag",
            "opposition.oversight.lag", "opp.oversight.2lag", "urban.2lag", "v2elmulpar", "v2elintmon", 
            "country_name", "reform_positive.lag", "loggpdpc.2lag", "reform_negative.lag",
            "democracy.duration.2lag", "exec.respectcon.2lag", "leg.constraints.2lag", 
            "altinfo.2lag", "loggpdpc.lag", "years.since.election",  "v2elparlel_1", "v2elparlel_2",  "v2elparlel_3", "v2elirreg.inv.lag") 

dataset.matching <- vdem.nodems.elections[myvars]
dataset.matching.complete <- na.omit(dataset.matching)


myvars <- c("democracy.duration.2lag", "oppaut.2lag",  "opp.oversight.2lag", "lji.2lag",
            "loggpdpc.2lag", "urban.2lag",
            "hc.ind.2lag",  "lc.ind.2lag", "transitional", "exec.respectcon.2lag", "altinfo.2lag", "education.2lag",
            "leg.constraints.2lag", "v2elirreg.inv.lag") 
ebal.covariates <- dataset.matching.complete[myvars]

ebal.test <- ebalance(Treatment=dataset.matching.complete$reform_positive.lag, X = ebal.covariates)




# means in treatment group data
t.means <- apply(ebal.covariates[dataset.matching.complete$reform_positive.lag==1,],2,mean)
# means in reweighted control group data
c.means.bal <- apply(ebal.covariates[dataset.matching.complete$reform_positive.lag==0,],2,weighted.mean,w=ebal.test$w)
# means in raw data control group data
c.means <- apply(ebal.covariates[dataset.matching.complete$reform_positive.lag==0,],2,mean)


dataset.matching.complete.controls <- subset(dataset.matching.complete, dataset.matching.complete$reform_positive.lag == 0)
dataset.matching.complete.controls <- data.frame(cbind(dataset.matching.complete.controls, ebal.test$w))


dataset.matching.complete.treat <- subset(dataset.matching.complete, dataset.matching.complete$reform_positive.lag == 1)
dataset.matching.complete.treat$ebal.test.w <- 1

dataset.matching.complete.w <- bind_rows(dataset.matching.complete.treat, dataset.matching.complete.controls)





mm.elirreg.posreform.oversight.lagman <- lm(v2elirreg.inv~reform_positive.lag +
                                             opposition.oversight.lag + reform_positive.lag*opposition.oversight.lag   + elexec + 
                                         loggpdpc.lag + 
                                              v2elintmon +
                                              v2elparlel_1 + v2elparlel_2   + reform_negative.lag + purge.lag + pack.lag+
                                      COW.factor, 
                                           weights=ebal.test.w,
                                           data = dataset.matching.complete.w)
#summary(mm.elirreg.posreform.oversight.lagman)

```

```{r}
p2.legov.lagman <- interplot(mm.elirreg.posreform.oversight.lagman, var1="reform_positive.lag", var2="opposition.oversight.lag",
                hist=TRUE, adjCI = TRUE) + theme_bw() + labs(x="Opposition oversight \n(1-year lag)", y="Marginal effect") +
  geom_hline(yintercept=0, linetype=2)   
p2.legov.lagman
```

Polcon


```{r ebal polcon lagman, include=FALSE}
vdem.nodems.elections <- vdem.nodems.elections %>% mutate(scale.polcon.lag = log(h_polcon3.lag))
myvars <- c("COW.factor", "year.factor", "transitional", "v2elirreg.inv", "v2elintim.inv", "lc.ind.2lag", "lji.2lag", "hc.ind.2lag", "oppaut.2lag", "elexec", "education.2lag",  "v2elparlel_1", "v2elparlel_2",                        "h_polcon3.lag", "h_polcon3.2lag",
             "urban.2lag",  "v2elintmon", "pack.lag", "purge.lag",
            "country_name", "reform_positive.lag", "loggpdpc.2lag", "reform_negative.lag",
            "democracy.duration.2lag", "exec.respectcon.2lag", "leg.constraints.2lag", 
            "altinfo.2lag", "loggpdpc.lag", "v2elirreg.inv.lag")  

dataset.matching <- vdem.nodems.elections[myvars]
dataset.matching.complete <- na.omit(dataset.matching)


myvars <- c("democracy.duration.2lag", "oppaut.2lag",  "h_polcon3.2lag", "lji.2lag",
             "loggpdpc.2lag", "urban.2lag",
            "hc.ind.2lag",  "lc.ind.2lag", "transitional", "exec.respectcon.2lag", "altinfo.2lag", "education.2lag",
            "leg.constraints.2lag", "v2elirreg.inv.lag") 
ebal.covariates <- dataset.matching.complete[myvars]

ebal.test <- ebalance(Treatment=dataset.matching.complete$reform_positive.lag, X = ebal.covariates)



# means in treatment group data
t.means <- apply(ebal.covariates[dataset.matching.complete$reform_positive.lag==1,],2,mean)
# means in reweighted control group data
c.means.bal <- apply(ebal.covariates[dataset.matching.complete$reform_positive.lag==0,],2,weighted.mean,w=ebal.test$w)
# means in raw data control group data
c.means <- apply(ebal.covariates[dataset.matching.complete$reform_positive.lag==0,],2,mean)



dataset.matching.complete.controls <- subset(dataset.matching.complete, dataset.matching.complete$reform_positive.lag == 0)
dataset.matching.complete.controls <- data.frame(cbind(dataset.matching.complete.controls, ebal.test$w))


dataset.matching.complete.treat <- subset(dataset.matching.complete, dataset.matching.complete$reform_positive.lag == 1)
dataset.matching.complete.treat$ebal.test.w <- 1

dataset.matching.complete.w <- bind_rows(dataset.matching.complete.treat, dataset.matching.complete.controls)


###regression
elirreg.posreform.polcon.base <- lm(v2elirreg.inv~reform_positive.lag +
                                        
                                        h_polcon3.lag  + elexec +  v2elparlel_1 + v2elparlel_2  +
                                            v2elintmon +
                                           reform_negative.lag + loggpdpc.lag + purge.lag + pack.lag+
                                      COW.factor, 
                                    weights=ebal.test.w,  
                                    data = dataset.matching.complete.w)
#summary(elirreg.posreform.polity.base)


mm.elirreg.posreform.polcon.lagman <- lm(v2elirreg.inv~reform_positive.lag
                                        + 
                                          h_polcon3.lag +
                                      reform_positive.lag*h_polcon3.lag
                                      + elexec + v2elparlel_1 + v2elparlel_2  +
                                              v2elintmon  +
                                               reform_negative.lag + loggpdpc.lag + purge.lag + pack.lag+
                                     COW.factor,  
                                            weights=ebal.test.w,
                                            data = dataset.matching.complete.w)
#summary(mm.elirreg.posreform.polcon.lagman)


```

```{r}
p4.polcon.lagman <- interplot(mm.elirreg.posreform.polcon.lagman, var1="reform_positive.lag", var2="h_polcon3.lag",
                hist=TRUE, adjCI = TRUE) + theme_bw() + labs(x="Political constraints (1-year lag)", y="Marginal effect") +
  geom_hline(yintercept=0, linetype=2)   
p4.polcon.lagman
```

## Producing Figure A.6
```{r combining me plots}
p.combined.lagman <- cowplot::plot_grid(p2.elcomp.lagman, p2.legov.lagman, p4.polcon.lagman, align = "hv", nrow = 2, label_size = 12, hjust = 0)
p.combined.lagman


```
## Producing Table A.8

```{r}
#Entropy balanced
stargazer(mm.elirreg.posreform.elcomp.lagman, mm.elirreg.posreform.oversight.lagman, mm.elirreg.posreform.polcon.lagman,
          digits = 2, omit = c("COW.factor", "COWcode", "year.factor", "year"),
          type="html", out="./Drafts/main results irreg ebal lagman.html")


```



# Lagged pro-government seat-share in selection model

Elcomp

```{r, include=FALSE}



myvars <- c("COW.factor", "year", "transitional", "v2elirreg.inv", "v2elintim.inv", "lc.ind.2lag",  "hc.ind.2lag", "oppaut.2lag", "elexec", "education.2lag",  "v2elparlel_1", "v2elparlel_2", "lji.2lag", "v2elparlel_3", "purge.lag", "pack.lag",
            "elcompindex.m.lag", "elcompindex.m.2lag", "urban.2lag",  "v2elintmon", 
            "country_name", "reform_positive.lag", "loggpdpc.2lag", "reform_negative.lag",
            "democracy.duration.2lag", "exec.respectcon.2lag", "leg.constraints.2lag", 
            "altinfo.2lag", "loggpdpc.lag", "govseat.frac.lag") 

dataset.matching <- vdem.nodems.elections[myvars]
dataset.matching.complete <- na.omit(dataset.matching)


myvars <- c("democracy.duration.2lag", "oppaut.2lag",  "elcompindex.m.2lag", "lji.2lag",
             "loggpdpc.2lag", "urban.2lag",
            "hc.ind.2lag",  "lc.ind.2lag", "transitional", "exec.respectcon.2lag", "altinfo.2lag", "education.2lag",
            "leg.constraints.2lag", "govseat.frac.lag") 
ebal.covariates <- dataset.matching.complete[myvars]

ebal.test <- ebalance(Treatment=dataset.matching.complete$reform_positive.lag, X = ebal.covariates)



# means in treatment group data
t.means <- apply(ebal.covariates[dataset.matching.complete$reform_positive.lag==1,],2,mean)
# means in reweighted control group data
c.means.bal <- apply(ebal.covariates[dataset.matching.complete$reform_positive.lag==0,],2,weighted.mean,w=ebal.test$w)
# means in raw data control group data
c.means <- apply(ebal.covariates[dataset.matching.complete$reform_positive.lag==0,],2,mean)



dataset.matching.complete.controls <- subset(dataset.matching.complete, dataset.matching.complete$reform_positive.lag == 0)
dataset.matching.complete.controls <- data.frame(cbind(dataset.matching.complete.controls, ebal.test$w))


dataset.matching.complete.treat <- subset(dataset.matching.complete, dataset.matching.complete$reform_positive.lag == 1)
dataset.matching.complete.treat$ebal.test.w <- 1

dataset.matching.complete.w <- bind_rows(dataset.matching.complete.treat, dataset.matching.complete.controls)





mm.elirreg.posreform.elcomp.govshare <- lm(v2elirreg.inv~reform_positive.lag
                                        + 
                                          elcompindex.m.lag +
                                      reform_positive.lag*elcompindex.m.lag
                                      + elexec + v2elparlel_1 + v2elparlel_2 + 
                                              # e_polity2 +
                                          loggpdpc.lag + 
                                              #e_miurbani + 
                                              v2elintmon +
                                          reform_negative.lag+ purge.lag + pack.lag+
                                     COW.factor,  
                                            weights=ebal.test.w,
                                            data = dataset.matching.complete.w)
#summary(mm.elirreg.posreform.elcomp.govshare)


```


```{r plot ebal elcomp}
p2.elcomp.govshare <- interplot(mm.elirreg.posreform.elcomp.govshare, var1="reform_positive.lag", var2="elcompindex.m.lag",
                hist=TRUE, adjCI = TRUE) + theme_bw() + labs(x="Political openness \n(1-year lag)", y="Marginal effect") +
  geom_hline(yintercept=0, linetype=2)   
p2.elcomp.govshare
```


Legislative oversight

```{r include=FALSE}
### Legislative opposition oversight
myvars <- c("COW.factor", "year.factor", "transitional", "v2elirreg.inv", "v2elintim.inv", "lc.ind.2lag", 
            "hc.ind.2lag", "oppaut.2lag", "elexec", "education.2lag",  "lji.2lag", "pack.lag", "purge.lag",
            "opposition.oversight.lag", "opp.oversight.2lag", "urban.2lag", "v2elmulpar", "v2elintmon", 
            "country_name", "reform_positive.lag", "loggpdpc.2lag", "reform_negative.lag",
            "democracy.duration.2lag", "exec.respectcon.2lag", "leg.constraints.2lag", 
            "altinfo.2lag", "loggpdpc.lag", "years.since.election",  "v2elparlel_1", "v2elparlel_2",  "v2elparlel_3", "v2elirreg.inv.lag", "govseat.frac.lag") 

dataset.matching <- vdem.nodems.elections[myvars]
dataset.matching.complete <- na.omit(dataset.matching)


myvars <- c("democracy.duration.2lag", "oppaut.2lag",  "opp.oversight.2lag", "lji.2lag",
            "loggpdpc.2lag", "urban.2lag",
            "hc.ind.2lag",  "lc.ind.2lag", "transitional", "exec.respectcon.2lag", "altinfo.2lag", "education.2lag",
            "leg.constraints.2lag", "v2elirreg.inv.lag", "govseat.frac.lag")  #"lji.lag"
ebal.covariates <- dataset.matching.complete[myvars]

ebal.test <- ebalance(Treatment=dataset.matching.complete$reform_positive.lag, X = ebal.covariates)




# means in treatment group data
t.means <- apply(ebal.covariates[dataset.matching.complete$reform_positive.lag==1,],2,mean)
# means in reweighted control group data
c.means.bal <- apply(ebal.covariates[dataset.matching.complete$reform_positive.lag==0,],2,weighted.mean,w=ebal.test$w)
# means in raw data control group data
c.means <- apply(ebal.covariates[dataset.matching.complete$reform_positive.lag==0,],2,mean)


dataset.matching.complete.controls <- subset(dataset.matching.complete, dataset.matching.complete$reform_positive.lag == 0)
dataset.matching.complete.controls <- data.frame(cbind(dataset.matching.complete.controls, ebal.test$w))


dataset.matching.complete.treat <- subset(dataset.matching.complete, dataset.matching.complete$reform_positive.lag == 1)
dataset.matching.complete.treat$ebal.test.w <- 1

dataset.matching.complete.w <- bind_rows(dataset.matching.complete.treat, dataset.matching.complete.controls)





mm.elirreg.posreform.oversight.govshare <- lm(v2elirreg.inv~reform_positive.lag +
                                             opposition.oversight.lag + reform_positive.lag*opposition.oversight.lag   + elexec + 
                                         loggpdpc.lag + 
                                              v2elintmon +
                                              v2elparlel_1 + v2elparlel_2   + reform_negative.lag + purge.lag + pack.lag+
                                      COW.factor, 
                                           weights=ebal.test.w,
                                           data = dataset.matching.complete.w)
#summary(mm.elirreg.posreform.oversight.govshare)

```

```{r}
p2.legov.govshare <- interplot(mm.elirreg.posreform.oversight.govshare, var1="reform_positive.lag", var2="opposition.oversight.lag",
                hist=TRUE, adjCI = TRUE) + theme_bw() + labs(x="Opposition oversight \n(1-year lag)", y="Marginal effect") +
  geom_hline(yintercept=0, linetype=2)   
p2.legov.govshare
```



Polcon


```{r ebal polcon seatshare, include=FALSE}
vdem.nodems.elections <- vdem.nodems.elections %>% mutate(scale.polcon.lag = log(h_polcon3.lag))
myvars <- c("COW.factor", "year.factor", "transitional", "v2elirreg.inv", "v2elintim.inv", "lc.ind.2lag", "lji.2lag", "hc.ind.2lag", "oppaut.2lag", "elexec", "education.2lag",  "v2elparlel_1", "v2elparlel_2",                        "h_polcon3.lag", "h_polcon3.2lag",
             "urban.2lag",  "v2elintmon", "pack.lag", "purge.lag",
            "country_name", "reform_positive.lag", "loggpdpc.2lag", "reform_negative.lag",
            "democracy.duration.2lag", "exec.respectcon.2lag", "leg.constraints.2lag", 
            "altinfo.2lag", "loggpdpc.lag", "govseat.frac.lag") 

dataset.matching <- vdem.nodems.elections[myvars]
dataset.matching.complete <- na.omit(dataset.matching)


myvars <- c("democracy.duration.2lag", "oppaut.2lag",  "h_polcon3.2lag", "lji.2lag",
             "loggpdpc.2lag", "urban.2lag",
            "hc.ind.2lag",  "lc.ind.2lag", "transitional", "exec.respectcon.2lag", "altinfo.2lag", "education.2lag",
            "leg.constraints.2lag", "govseat.frac.lag")
ebal.covariates <- dataset.matching.complete[myvars]

ebal.test <- ebalance(Treatment=dataset.matching.complete$reform_positive.lag, X = ebal.covariates)



# means in treatment group data
t.means <- apply(ebal.covariates[dataset.matching.complete$reform_positive.lag==1,],2,mean)
# means in reweighted control group data
c.means.bal <- apply(ebal.covariates[dataset.matching.complete$reform_positive.lag==0,],2,weighted.mean,w=ebal.test$w)
# means in raw data control group data
c.means <- apply(ebal.covariates[dataset.matching.complete$reform_positive.lag==0,],2,mean)



dataset.matching.complete.controls <- subset(dataset.matching.complete, dataset.matching.complete$reform_positive.lag == 0)
dataset.matching.complete.controls <- data.frame(cbind(dataset.matching.complete.controls, ebal.test$w))


dataset.matching.complete.treat <- subset(dataset.matching.complete, dataset.matching.complete$reform_positive.lag == 1)
dataset.matching.complete.treat$ebal.test.w <- 1

dataset.matching.complete.w <- bind_rows(dataset.matching.complete.treat, dataset.matching.complete.controls)



mm.elirreg.posreform.polcon.govshare <- lm(v2elirreg.inv~reform_positive.lag
                                        + 
                                          h_polcon3.lag +
                                      reform_positive.lag*h_polcon3.lag
                                      + elexec + v2elparlel_1 + v2elparlel_2  +
                                                                  v2elintmon  +
                                               reform_negative.lag + loggpdpc.lag + purge.lag + pack.lag+
                                     COW.factor,  
                                            weights=ebal.test.w,
                                            data = dataset.matching.complete.w)
#summary(mm.elirreg.posreform.polcon.govshare)


```

```{r}
p4.polcon.govshare <- interplot(mm.elirreg.posreform.polcon.govshare, var1="reform_positive.lag", var2="h_polcon3.lag",
                hist=TRUE, adjCI = TRUE) + theme_bw() + labs(x="Political constraints (1-year lag)", y="Marginal effect") +
  geom_hline(yintercept=0, linetype=2)   
p4.polcon.govshare
```
## Producing Table A.9

```{r}
stargazer(mm.elirreg.posreform.elcomp.govshare, mm.elirreg.posreform.oversight.govshare, mm.elirreg.posreform.polcon.govshare,
          digits = 2, omit = c("COW.factor", "COWcode", "year.factor", "year"),
          type="text")


```
## Producing Figure A.7

```{r combining govshare plots}
p.combined.ebal.gs <- cowplot::plot_grid(p2.elcomp.govshare, p2.legov.govshare, p4.polcon.govshare, align = "hv", nrow = 2, label_size = 12, hjust = 0)
p.combined.ebal.gs

```



## Judicial access--  CCP treatments

```{r ebal elcomp, include=FALSE}



myvars <- c("COW.factor", "year", "transitional", "v2elirreg.inv", "v2elintim.inv", "lc.ind.2lag",  "hc.ind.2lag", "oppaut.2lag", "elexec", "education.2lag",  "v2elparlel_1", "v2elparlel_2", "lji.2lag", "v2elparlel_3", "purge.lag", "pack.lag",
            "elcompindex.m.lag", "elcompindex.m.2lag", "urban.2lag",  "v2elintmon", 
            "country_name", "reform_positive.lag", "loggpdpc.2lag", "reform_negative.lag",
            "democracy.duration.2lag", "exec.respectcon.2lag", "leg.constraints.2lag", 
            "altinfo.2lag", "loggpdpc.lag", "citizen.access") 

dataset.matching <- vdem.nodems.elections[myvars]
dataset.matching.complete <- na.omit(dataset.matching)


myvars <- c("democracy.duration.2lag", "oppaut.2lag",  "elcompindex.m.2lag", "lji.2lag",
             "loggpdpc.2lag", "urban.2lag",
            "hc.ind.2lag",  "lc.ind.2lag", "transitional", "exec.respectcon.2lag", "altinfo.2lag", "education.2lag",
            "leg.constraints.2lag")  
ebal.covariates <- dataset.matching.complete[myvars]

ebal.test <- ebalance(Treatment=dataset.matching.complete$reform_positive.lag, X = ebal.covariates)



# means in treatment group data
t.means <- apply(ebal.covariates[dataset.matching.complete$reform_positive.lag==1,],2,mean)
# means in reweighted control group data
c.means.bal <- apply(ebal.covariates[dataset.matching.complete$reform_positive.lag==0,],2,weighted.mean,w=ebal.test$w)
# means in raw data control group data
c.means <- apply(ebal.covariates[dataset.matching.complete$reform_positive.lag==0,],2,mean)



dataset.matching.complete.controls <- subset(dataset.matching.complete, dataset.matching.complete$reform_positive.lag == 0)
dataset.matching.complete.controls <- data.frame(cbind(dataset.matching.complete.controls, ebal.test$w))


dataset.matching.complete.treat <- subset(dataset.matching.complete, dataset.matching.complete$reform_positive.lag == 1)
dataset.matching.complete.treat$ebal.test.w <- 1

dataset.matching.complete.w <- bind_rows(dataset.matching.complete.treat, dataset.matching.complete.controls)


###regression



mm.elirreg.posreform.access <- lm(v2elirreg.inv~reform_positive.lag
                                        + 
                                          elcompindex.m.lag +
                                      reform_positive.lag*elcompindex.m.lag
                                      + elexec + v2elparlel_1 + v2elparlel_2 + 
                                          loggpdpc.lag + 
                                              v2elintmon +
                                          reform_negative.lag+ purge.lag + pack.lag+ citizen.access +
                                     COW.factor,  
                                            weights=ebal.test.w,
                                            data = dataset.matching.complete.w)
#summary(mm.elirreg.posreform.access)


```

```{r}
p2.elcomp.access <- interplot(mm.elirreg.posreform.access, var1="reform_positive.lag", var2="elcompindex.m.lag",
                hist=TRUE, adjCI = TRUE) + theme_bw() + labs(x="Political openness \n(1-year lag)", y="Marginal effect") +
  geom_hline(yintercept=0, linetype=2)   #No effect for fraud
p2.elcomp.access
```


```{r include=FALSE}
### Legislative opposition oversight
myvars <- c("COW.factor", "year.factor", "transitional", "v2elirreg.inv", "v2elintim.inv", "lc.ind.2lag", 
            "hc.ind.2lag", "oppaut.2lag", "elexec", "education.2lag",  "lji.2lag", "pack.lag", "purge.lag",
            "opposition.oversight.lag", "opp.oversight.2lag", "urban.2lag", "v2elmulpar", "v2elintmon", 
            "country_name", "reform_positive.lag", "loggpdpc.2lag", "reform_negative.lag",
            "democracy.duration.2lag", "exec.respectcon.2lag", "leg.constraints.2lag", 
            "altinfo.2lag", "loggpdpc.lag", "years.since.election",  "v2elparlel_1", "v2elparlel_2",  "v2elparlel_3", "citizen.access")  
dataset.matching <- vdem.nodems.elections[myvars]
dataset.matching.complete <- na.omit(dataset.matching)


myvars <- c("democracy.duration.2lag", "oppaut.2lag",  "opp.oversight.2lag", "lji.2lag",
            "loggpdpc.2lag", "urban.2lag",
            "hc.ind.2lag",  "lc.ind.2lag", "transitional", "exec.respectcon.2lag", "altinfo.2lag", "education.2lag",
            "leg.constraints.2lag") 
ebal.covariates <- dataset.matching.complete[myvars]

ebal.test <- ebalance(Treatment=dataset.matching.complete$reform_positive.lag, X = ebal.covariates)




# means in treatment group data
t.means <- apply(ebal.covariates[dataset.matching.complete$reform_positive.lag==1,],2,mean)
# means in reweighted control group data
c.means.bal <- apply(ebal.covariates[dataset.matching.complete$reform_positive.lag==0,],2,weighted.mean,w=ebal.test$w)
# means in raw data control group data
c.means <- apply(ebal.covariates[dataset.matching.complete$reform_positive.lag==0,],2,mean)


dataset.matching.complete.controls <- subset(dataset.matching.complete, dataset.matching.complete$reform_positive.lag == 0)
dataset.matching.complete.controls <- data.frame(cbind(dataset.matching.complete.controls, ebal.test$w))


dataset.matching.complete.treat <- subset(dataset.matching.complete, dataset.matching.complete$reform_positive.lag == 1)
dataset.matching.complete.treat$ebal.test.w <- 1

dataset.matching.complete.w <- bind_rows(dataset.matching.complete.treat, dataset.matching.complete.controls)


###regression
elirreg.posreform.oversight.base.ccp <- lm(v2elirreg.inv~reform_positive.lag +
                                               opposition.oversight.lag + elexec + 
                                           loggpdpc.lag + 
                                               v2elintmon + 
                                            v2elparlel_1 + v2elparlel_2 +  reform_negative.lag + purge.lag + pack.lag+
                                         COW.factor 
                                             , weights=ebal.test.w,
                                             data = dataset.matching.complete.w)
#summary(elirreg.posreform.oversight.base.ccp)


mm.elirreg.access.oversight.ccp <- lm(v2elirreg.inv~ 
                                              reform_positive.lag*opposition.oversight.lag   + elexec + 
                                         loggpdpc.lag + 
                                              v2elintmon +
                                              v2elparlel_1 + v2elparlel_2   + reform_negative.lag + purge.lag + pack.lag+ citizen.access +

                                      COW.factor, 
                                           weights=ebal.test.w,
                                           data = dataset.matching.complete.w)
#summary(mm.elirreg.access.oversight.ccp)

```

```{r}
p2.legov.access <- interplot(mm.elirreg.access.oversight.ccp, var1="reform_positive.lag", var2="opposition.oversight.lag",
                hist=TRUE, adjCI = TRUE) + theme_bw() + labs(x="Opposition oversight \n(1-year lag)", y="Marginal effect") +
  geom_hline(yintercept=0, linetype=2)   
p2.legov.access
```


```{r ebal polcon, include=FALSE}
vdem.nodems.elections <- vdem.nodems.elections %>% mutate(scale.polcon.lag = log(h_polcon3.lag))
myvars <- c("COW.factor", "year.factor", "transitional", "v2elirreg.inv", "v2elintim.inv", "lc.ind.2lag", "lji.2lag", "hc.ind.2lag", "oppaut.2lag", "elexec", "education.2lag",  "v2elparlel_1", "v2elparlel_2",                        "h_polcon3.lag", "h_polcon3.2lag",
             "urban.2lag",  "v2elintmon", "pack.lag", "purge.lag",
            "country_name", "reform_positive.lag", "loggpdpc.2lag", "reform_negative.lag",
            "democracy.duration.2lag", "exec.respectcon.2lag", "leg.constraints.2lag", 
            "altinfo.2lag", "loggpdpc.lag", "citizen.access")  

dataset.matching <- vdem.nodems.elections[myvars]
dataset.matching.complete <- na.omit(dataset.matching)


myvars <- c("democracy.duration.2lag", "oppaut.2lag",  "h_polcon3.2lag", "lji.2lag",
             "loggpdpc.2lag", "urban.2lag",
            "hc.ind.2lag",  "lc.ind.2lag", "transitional", "exec.respectcon.2lag", "altinfo.2lag", "education.2lag",
            "leg.constraints.2lag") 
ebal.covariates <- dataset.matching.complete[myvars]

ebal.test <- ebalance(Treatment=dataset.matching.complete$reform_positive.lag, X = ebal.covariates)



# means in treatment group data
t.means <- apply(ebal.covariates[dataset.matching.complete$reform_positive.lag==1,],2,mean)
# means in reweighted control group data
c.means.bal <- apply(ebal.covariates[dataset.matching.complete$reform_positive.lag==0,],2,weighted.mean,w=ebal.test$w)
# means in raw data control group data
c.means <- apply(ebal.covariates[dataset.matching.complete$reform_positive.lag==0,],2,mean)



dataset.matching.complete.controls <- subset(dataset.matching.complete, dataset.matching.complete$reform_positive.lag == 0)
dataset.matching.complete.controls <- data.frame(cbind(dataset.matching.complete.controls, ebal.test$w))


dataset.matching.complete.treat <- subset(dataset.matching.complete, dataset.matching.complete$reform_positive.lag == 1)
dataset.matching.complete.treat$ebal.test.w <- 1

dataset.matching.complete.w <- bind_rows(dataset.matching.complete.treat, dataset.matching.complete.controls)


###regression
elirreg.access.polcon.base <- lm(v2elirreg.inv~reform_positive.lag +
                                        
                                        h_polcon3.lag  + elexec +  v2elparlel_1 + v2elparlel_2  +
                                                                        v2elintmon +
                                           reform_negative.lag + loggpdpc.lag + purge.lag + pack.lag+
                                      COW.factor, 
                                    weights=ebal.test.w,  
                                    data = dataset.matching.complete.w)
#summary(elirreg.posreform.polity.base)


mm.elirreg.access.polcon.ccp <- lm(v2elirreg.inv~reform_positive.lag
                                        + 
                                          h_polcon3.lag +
                                      reform_positive.lag*h_polcon3.lag
                                      + elexec + v2elparlel_1 + v2elparlel_2  +
                                         
                                              v2elintmon  +
                                               reform_negative.lag + loggpdpc.lag + purge.lag + pack.lag+ citizen.access +
                                     COW.factor,  
                                            weights=ebal.test.w,
                                            data = dataset.matching.complete.w)
#summary(mm.elirreg.access.polcon.ccp)


```




```{r}
p4.polcon.access <- interplot(mm.elirreg.access.polcon.ccp, var1="reform_positive.lag", var2="h_polcon3.lag",
                hist=TRUE, adjCI = TRUE) + theme_bw() + labs(x="Political constraints (1-year lag)", y="Marginal effect") +
  geom_hline(yintercept=0, linetype=2)   
p4.polcon.access
```

## Producing Table A.10

```{r}
stargazer(mm.elirreg.posreform.access, mm.elirreg.access.oversight.ccp, mm.elirreg.access.polcon.ccp, type="text", 
                  omit = c("COW.factor", "COWcode", "year.factor", "year"))
```

## Producing Figure A.8
```{r combining me plots}
p.combined.ebal.access <- cowplot::plot_grid(p2.elcomp.access, p2.legov.access, p4.polcon.access, align = "hv", nrow = 2, label_size = 12, hjust = 0)
p.combined.ebal.access


```
